A database query returns several rows which I loop through as follows:
foreach ($query->result() as $row) {
$data[$row->post_id]['post_id'] = $row->post_id;
$data[$row->post_id]['post_type'] = $row->post_type;
$data[$row->post_id]['post_text'] = $row->post_text;
}
If I json_encode the resulting array ($a['stream']) I get
{
"stream": {
"1029": {
"post_id": "1029",
"post_type": "1",
"post_text": "bla1",
},
"1029": {
"post_id": "1030",
"post_type": "3",
"post_text": "bla2",
},
"1029": {
"post_id": "1031",
"post_type": "2",
"post_text": "bla3",
}
}
}
But the json should actually look like this:
{
"stream": {
"posts": [{
"post_id": "1029",
"post_type": "1",
"post_text": "bla1",
},
{
"post_id": "1030",
"post_type": "3",
"post_text": "bla2",
},
{
"post_id": "1031",
"post_type": "2",
"post_text": "bla3",
}]
}
}
How should I build my array to get this json right?