$tmp = [];
$tmp['id'] = 'aaa';
$tmp['name'] = 'bbb';
$tmp['vvvv'] = [
'www'=>1,
'ffff'=>2
];
echo (json_encode($tmp));
The output is
{
"id": "aaa",
"name": "bbb",
"vvvv": {
"www": 1,
"ffff": 2
}
}
I would like to ask how to change the output to
{
"id": "aaa",
"name": "bbb",
"vvvv": [
{"www": 1},
{"ffff": 2}
]
}
I have tested that this works, but it is not very easy to use:
$tmp['vvvv'] = [
0=>(object)['www'=>1],
1=>(object)['fff'=>2]
];
phpcn_u15822017-06-30 09:57:13
Just replace the elements in $tmp['vvvv'] with an array. There is no need to convert them into objects, as follows
$tmp = [];
$tmp['id'] = 'aaa';
$tmp['name'] = 'bbb';
$tmp['vvvv'] = [
['www'=>1],
['ffff'=>2]
];