データベースクエリは、次のようにループするいくつかの行を返します。
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
ますか?