I have this Json(see below) and I am trying actually to find a way to generate one like this using a php array. My question is how can I generate a json like this from a php array. in another way, I still don't know how to build that php array so that I can convert it to this a json like the one below. I am using symfony2 to render this a json like this one
var presentation = [{
"image": "images/cover.jpg",
"sentence": "This is a sentence",
"audio": "hello.mp3",
"sentence_info": [
{"start": 0, "end": 0.5 },
{ "start": 0.5, "end": 1.2 }
]
},
{
"image": "images/cat.jpg",
"sentence": "This is another sentence",
"audio": "bey.mp3",
"sentence_info": [
{ "start": 0, "end": 0.2 },
{ "start": 0.2, "end": 0.8 },
{ "start": 0.8, "end": 1.2 },
{ "start": 1.2, "end": 1.5 },
{ "start": 1.5, "end": 2 }
]
},
{
"image": "images/dog.jpg",
"sentence": "This is a third sentence",
"audio": "good.mp3",
"sentence_info": [
{ "start": 0, "end": 0.5 },
{ "start": 0.5, "end": 1.2 }
]
}
];
This is just simple array assignment in PHP. Like this:
public function whatEver()
{
$presentation = array(
array(
'image' => 'images/cover.jpg',
'sentence' => 'This is a sentence',
'audio' => 'hello.mp3',
'sentence_info' => array(
array('start' => 0, 'end' => 0.5),
array('start' => 0.5, 'end' => 1.2),
),
),
array(
'image' => 'images/cat.jpg',
'sentence' => 'This is another sentence',
'audio' => 'bey.mp3',
'sentence_info' => array(
array('start' => 0, 'end' => 0.2),
array('start' => 0.2, 'end' => 0.8),
array('start' => 0.8, 'end' => 1.2),
array('start' => 1.2, 'end' => 1.5),
array('start' => 1.5, 'end' => 2),
),
),
array(
'image' => 'images/dog.jpg',
'sentence' => 'This is a third sentence',
'audio' => 'good.mp3',
'sentence_info' => array(
array('start' => 0, 'end' => 0.5),
array('start' => 0.5, 'end' => 1.2),
),
),
);
$presentation = json_encode($presentation, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
$response = new Response($presentation);
$response->headers->set('Content-Type', 'application/json');
return $response;
}
You would use json_encode
on an array you've built up.
A simple look at the supplied json would should you that it is an array []
with various objects {}
inside.
You'd be looping through data from wherever you get it from (most likely a database?) and then adding it to the array like this:
$data = array();
foreach ($data_from_wherever as $item) {
$i = array(
'image' => '',
'sentence' => '',
'audio' => '',
'sentence_info' => array(
array(
'start' => '',
'finish' => ''
),
array(
'start' => '',
'finish' => ''
),
// ......etcetc
),
);
array_push($data, $i);
}
Giving you an array that you'd json_encode()
to acheive your desired output.
$presentation = json_encode($data);
And if you don't know how to get said data yet, you can simply create the $data
array like below and then encode it.
$data = array(
array(
'image' => 'images/cover.jpg',
'sentence' => 'This is a sentence',
'audio' => 'hello.mp3',
'sentence_info' => array(
array('start' => 0, 'end' => 0.5),
array('start' => 0.5, 'end' => 1.2),
),
),
array(
'image' => 'images/cat.jpg',
'sentence' => 'This is another sentence',
'audio' => 'bey.mp3',
'sentence_info' => array(
array('start' => 0, 'end' => 0.2),
array('start' => 0.2, 'end' => 0.8),
array('start' => 0.8, 'end' => 1.2),
array('start' => 1.2, 'end' => 1.5),
array('start' => 1.5, 'end' => 2),
),
),
array(
'image' => 'images/dog.jpg',
'sentence' => 'This is a third sentence',
'audio' => 'good.mp3',
'sentence_info' => array(
array('start' => 0, 'end' => 0.5),
array('start' => 0.5, 'end' => 1.2),
),
),
);