【发布时间】:2021-08-29 00:23:47
【问题描述】:
我有一个嵌套的 json 对象数组:
$json_url_data = [{
"form_name": "z1",
"name":"name1",
"children": [{
"form_name": "z2",
"name":"name2",
"children": [{
"form_name": "z3",
"name":"name2"
}]
}]
}]
我想动态插入一个新的键值对,它应该看起来像这样:
[{
"form_name": "z1",
"name":"name1",
"peopleCount": "125,678,190",
"children": [{
"form_name": "z2",
"name":"name2",
"peopleCount": "156,987",
"children": [{
"form_name": "z3",
"name":"name2",
"peopleCount": "678,098"
}]
}]
}]
我尝试使用如下递归,但无法达到上述结果:
function print_data($menu, $depth) {
foreach ($menu as $value) {
$deepid = '\"'.$value['form_name'].'\"';
$api_call_var = '{{i have a apiu call proxy here}}';
$read_data = file_get_contents($read_data);
$read_data = json_decode($read_data, true);
$value['peopleCount'] = $read_data['count']; // after decode read the count
$obj_array[] = $value;
if (is_array($value['children'])) {
print_data ($value['children'],$depth+1);
}
}//end of for each
}//end of function
print_data ($json_url_data, 0);
我被困了几个小时,任何帮助将不胜感激。
【问题讨论】:
标签: php arrays json api recursion