数据库查询返回几行,我循环如下:
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;
}
如果我json_encode
得到的数组 ( $a['stream']
) 我得到
{
"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",
}
}
}
但json
实际上应该是这样的:
{
"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",
}]
}
}
我应该如何构建我的阵列才能做到这一点json
?