數據庫查詢返回幾個行,我遍歷如下:需要幫助建立JSON數組
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
對不對?
我看不到任何'$了' – Alexander