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 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建