$tmp = [];
$tmp['id'] = 'aaa';
$tmp['name'] = 'bbb';
$tmp['vvvv'] = [
'www'=>1,
'ffff'=>2
];
echo (json_encode($tmp));
輸出是
{
"id": "aaa",
"name": "bbb",
"vvvv": {
"www": 1,
"ffff": 2
}
}
我想問如何將輸出改為
{
"id": "aaa",
"name": "bbb",
"vvvv": [
{"www": 1},
{"ffff": 2}
]
}
我測試過這樣是可以的,就是不太好用:
$tmp['vvvv'] = [
0=>(object)['www'=>1],
1=>(object)['fff'=>2]
];
phpcn_u15822017-06-30 09:57:13
將$tmp['vvvv']裡的元素換成數組就行了,不需要轉換為object,如下
$tmp = [];
$tmp['id'] = 'aaa';
$tmp['name'] = 'bbb';
$tmp['vvvv'] = [
['www'=>1],
['ffff'=>2]
];