我试图将孩子附加到JSON中的相应父级... 以下是我的代码: 我在$ json1中有一个json,在$ json2中有多个json。 $ json1和$ json2 - https://pastebin.com/DTn5EPxq
<?php
$djson1 = json_decode($json1);
$djson2 = json_decode($json2, true);
$result['id'] = $djson1->{'id'};
$result['name'] = $djson1->{'name'};
$result['tooltip'] = $djson1->{'tooltip'};
$result['color'] = $djson1->{'color'};
foreach ($djson1->{'children'} as $key => $value) {
$result['children'][$key]['name'] = $value->{'name'};
$result['children'][$key]['parent'] = $value->{'parent'};
$result['children'][$key]['tooltip'] = $value->{'tooltip'};
$result['children'][$key]['color'] = $value->{'color'};
}
foreach ($djson2 as $key2 => $value2) {
if($value2['name'] == $result['children'][$key2]['name']) {
foreach ($value2['children'] as $key3 => $value3) {
$result['children'][$key2]['children']
[$key3]['name'] = $value3['name'];
$result['children'][$key2]['children']
[$key3]['parent'] = $value3['parent'];
$result['children'][$key2]['children']
[$key3]['tooltip'] = $value3['tooltip'];
$result['children'][$key2]['children']
[$key3]['color'] = $value3['color'];
}
}
}
//var_dump($result);
//var_dump(json_encode($result));
?>
<?php echo json_encode($result,JSON_UNESCAPED_UNICODE); ?>
我在此行中收到未定义的偏移量错误:
if($value2['name'] == $result['children'][$key2]['name']) {
这个代码效果很好,当我在$ json2中使用少量json时..显示错误只有在$ json2中使用大量json时才会解决这个问题???
答案 0 :(得分:0)
因为$result['children'][2]['name']
没有密钥2。它只有0和1所以它是未定义的索引
$ result数组
$result = Array ( [id] => 1
[name] => கà¯à®´à¯‚உ
[tooltip] => [color] =>
[children] => Array ( [0] => Array ( [name] => கூடà¯à®Ÿà®®à¯ [parent] => 1 [tooltip] => [color] => #FF0000 )
[1] => Array ( [name] => [parent] => 1 [tooltip] => [color] => #FFFFCC )
)
)
更新1:
使用array_column
获取所有孩子的姓名并使用array_search
如果匹配则返回密钥。所以使用该键可以像这样推送数组
$new_array = array_column($result['children'],'name');
foreach ($djson2 as $key2 => $value2) {
if($new_key = array_search($value2['name'],$new_array))
{
foreach ($value2['children'] as $key3 => $value3)
{
$result['children'][$new_key]['children'][$key3]['name'] = $value3['name'];
$result['children'][$new_key]['children'][$key3]['parent'] = $value3['parent'];
$result['children'][$new_key]['children'][$key3]['tooltip'] = $value3['tooltip'];
$result['children'][$new_key]['children'][$key3]['color'] = $value3['color'];
}
}
}
更新2:
问题是我们的$json1 name is "கூட்டம் "
有空格但$json2 name is "கூட்டம்"
这就是为什么它不会进入if条件只是用户array_map
来修剪这样的空间。
$array1 = array_column($result['children'],'name');
$new_array = array_map('trim',$array1);
foreach ($djson2 as $key2 => $value2) {
$true_or_false_key = array_search($value2['name'],$new_array);
if($true_or_false_key !== false)
{
foreach ($value2['children'] as $key3 => $value3)
{
$result['children'][ $true_or_false_key]['children'][$key3]['name'] = $value3['name'];
$result['children'][ $true_or_false_key]['children'][$key3]['parent'] = $value3['parent'];
$result['children'][ $true_or_false_key]['children'][$key3]['tooltip'] = $value3['tooltip'];
$result['children'][ $true_or_false_key]['children'][$key3]['color'] = $value3['color'];
}
}
}