drcj64241 2018-05-20 17:27
浏览 39
已采纳

使用相同的ID在PHP中组合两个多维数组

I want to add values from one array to another that are both potentially multidimensional when they have the same ID. Hard to explain so I added example.

arr1 => new structure, without full data

arr2 => old structure, with full data

Examples if you'd like to help out:

// arr1 (the correct structure/order, without the full data)
[{
    "id": "24",
    "children": [{
        "id": "21",
        "children": [{
            "id": "15"
        }]
    }]
}]



// arr2 (full data, not in any specific order, may be nested)
[{
    "id": "24",
    "name": " x",
    "time": "0",
    "status": "0"
}, {
    "id": "21",
    "children": [{
        "id": "15",
        "name": "x",
        "time": "0",
        "status": "0"
    }],
    "name": "x",
    "time": "0",
    "status": "0"
}]


// arr3 (desired output for this example)
[{
    "id": "24",
    "children": [{
        "id": "21",
        "children": [{
            "id": "15",
            "name": "x",
            "time": "0",
            "status": "0"
        }],
        "name": "x",
        "time": "0",
        "status": "0"
    }],
    "name": " x",
    "time": "0",
    "status": "0"
}]

I tried this:

    function merge($arr1, $arr2) {
        foreach($arr1 as $key => $value){
            foreach($arr2 as $value2) {
                if($value['id'] === $value2['id']){
                    $arr1[$key]['name'] = $value2['name'];
                    $arr1[$key]['time'] = $value2['time'];
                    $arr1[$key]['status'] = $value2['status'];
                    if (is_array($value)) {
                        $arr1[$key]['children'] = merge($arr1, $arr2);
                    }
                }

            }
        }
        return $arr1;
    }

to combine them, but I can't figure out how to handle the nesting correctly. I have tried a lot of other things as well like using array_merge_recursive() but it doesn't work because I want to merge based on ID value. Any help on getting me on track would be awesome thanks.

Current output for example:

[{
    "id": "24",
    "children": [{
        "id": "21",
        "children": [{
            "id": "15"
        }]
    }],
    "name": " x",
    "time": "0",
    "status": "0"
}]

Desired output for example:

[{
    "id": "24",
    "children": [{
        "id": "21",
        "children": [{
            "id": "15",
            "name": "x",
            "time": "0",
            "status": "0"
        }],
        "name": "x",
        "time": "0",
        "status": "0"
    }],
    "name": " x",
    "time": "0",
    "status": "0"
}]
  • 写回答

2条回答 默认 最新

  • doutu2017 2018-05-20 18:48
    关注

    Edit: How about this?

    $detailsClean = [];
    
    foreach($array2 as $item) {
        $detailsClean = removeDepth($item, $detailsClean);
    }
    
    
    foreach($array1 as $itemKey => $item) {
        $array1[$itemKey] = addDetails($item, $detailsClean);
    }
    
    
    function removeDepth($array, $result) {
        $id = $array['id'];
        if (!empty($array['children'])) {
            foreach($array['children'] as $child) {
                $result = removeDepth($child, $result);
            }
            unset($array['children']);
        }
        $result[$id] = $array;
    
        return $result;
    }
    
    function addDetails($array, $details) {
        $id = $array['id'];
        if (isset($details[$id])) {
            $array = array_merge($array, $details[$id]);
            if (!empty($array['children'])) {
                foreach($array['children'] as $childKey => $child) {
                    $array['children'][$childKey] = addDetails($child, $details);
                }
            }
        }
        return $array;
    }
    

    $array1 is updated with the final result.

    Here is an example with the data from your unedited post: http://phpio.net/s/7z09

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器