Need help setting up json array
Posted
by
torr
on Stack Overflow
See other posts from Stack Overflow
or by torr
Published on 2012-09-15T21:24:02Z
Indexed on
2012/09/15
21:37 UTC
Read the original article
Hit count: 193
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?
© Stack Overflow or respective owner