PHP outputs json, which requires nested arrays and objects.
为情所困
为情所困 2017-06-30 09:55:13
0
2
654
            $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]
            ];
为情所困
为情所困

reply all(2)
phpcn_u1582

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]
            ];
伊谢尔伦

That’s how it is, all PHP is map

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!