[英]Json array of objects in php
this is the json that my code produces 这是我的代码产生的json
{
"aaa":1,
"b":2,
"c":3,
"d":4,
"e":5,
"fff":{"a":11111,"b":222222,"c":33333,"d":444454,"e":55555555}
}
and this is the code 这是代码
<?php
$c = array('a' => 11111, 'b' => 222222, 'c' => 33333, 'd' => 444454, 'e' => 55555555 );
$arr = array('aaa' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5 , 'fff'=>$c);
echo json_encode($arr);
?>
but I want to have some structure like this 但我想有这样的结构
{
"aaa":1,
"b":2,
"c":3,
"d":4,
"e":5,
"fff":{"a":11111,"b":222222,"c":33333,"d":444454,"e":55555555},
"last":[
{
"id": 8817,
"loc": "NEW YORK CITY"
},
{
"id": 2873,
"loc": "UNITED STATES"
},
{
"id": 1501,
"loc": "NEW YORK STATE"
}
]
}
I am new in json and php and I need this fast so I do not have time to read about this json structure... So please if someone know how to add this last element please provide some php code. 我是json和php的新手,所以我需要这么快,所以我没有时间阅读这个json结构...因此,如果有人知道如何添加最后一个元素,请提供一些php代码。
Thanks, 谢谢,
eg 例如
<?php
$json = '{
"aaa":1,
"b":2,
"c":3,
"d":4,
"e":5,
"fff":{"a":11111,"b":222222,"c":33333,"d":444454,"e":55555555},
"last":[
{
"id": 8817,
"loc": "NEW YORK CITY"
},
{
"id": 2873,
"loc": "UNITED STATES"
},
{
"id": 1501,
"loc": "NEW YORK STATE"
}
]
}';
$php = json_decode($json, true);
echo var_export($php);
prints 版画
array (
'aaa' => 1,
'b' => 2,
'c' => 3,
'd' => 4,
'e' => 5,
'fff' =>
array (
'a' => 11111,
'b' => 222222,
'c' => 33333,
'd' => 444454,
'e' => 55555555,
),
'last' =>
array (
0 =>
array (
'id' => 8817,
'loc' => 'NEW YORK CITY',
),
1 =>
array (
'id' => 2873,
'loc' => 'UNITED STATES',
),
2 =>
array (
'id' => 1501,
'loc' => 'NEW YORK STATE',
),
),
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.