Laravel根据ID键将来自另一个数组的数据合并到嵌套数组中

时间:2018-12-06 05:57:44

标签: php laravel multidimensional-array nested-loops

尝试通过最有效的Laravel集合或数组助手来解决此问题。有人可以帮忙吗?

$menu = [
    ["id"=>1],
    ["id"=>2],
    ["id"=>3,"children"=>[
        ["id"=>4]
    ]],
    ["id"=>5,"children"=>[
        ["id"=>6],
        ["id"=>7,"children"=>[
            ["id"=>8],
            ["id"=>9]
        ]]
    ]],
    ["id"=>10]
];

$pages = [
    ['id'=>1,'label'=>'About Us','url_path'=>'/about-us'],
    ['id'=>2,'label'=>'Meet the Team','url_path'=>'/meet-the-team'],
    ['id'=>3,'label'=>'Services','url_path'=>'/services'],
    ['id'=>4,'label'=>'Contact Us','url_path'=>'/contact-us'],
    ['id'=>5,'label'=>'Company Mission','url_path'=>'/company-mission'],
    ['id'=>6,'label'=>'History','url_path'=>'/history'],
    ...
    ...
];

嵌套数组$ menu中的ID键需要基于相同的ID搜索$ pages数组,然后将整行合并回$ menu。

我的功能:

$menu = collect($menu)->map(function($item) use($pages){
    $page_item = collect($pages)->firstWhere('id',$item['id']);
    if(array_has($item,'children')){
        $page_item['children'] = ...
    }
    return $page_item;
})->toArray();

如何通过递归迭代将数组数据从$ pages添加到“ children”?有没有更好的方法来使此功能更有效?我想利用Laravel集合或数组助手来解决此问题,而不是使用foreach循环等。

以下是预期结果:

$menu = [
    ["id"=>1,"label"=>"About Us","url_path"=>"/about-us"],
    ["id"=>2,"label"=>"Meet the Team","url_path"=>"/meet-the-team"],
    ["id"=>3,"label"=>"Services","url_path"=>"/services","children"=>[
        ["id"=>4,"label"=>"Contact Us","url_path"=>"/contact-us"]
    ]],
    ["id"=>5,"label"=>"Company Mission","url_path"=>"/company-mission","children"=>[
        ["id"=>6,"label"=>"History","url_path"=>"/history"],
        ["id"=>7, ...
    ...
];

有人可以告诉我吗?谢谢!

1 个答案:

答案 0 :(得分:2)

您可以通过使用递归函数来实现此目的,

function map_pages_to_menu(array $pages, array $menu) {
    return collect($menu)->map(function ($menu_item) use ($pages){

        // find the page item that matches menu id
        $page_item = collect($pages)->firstWhere('id',$menu_item['id']);

        // if there is a match create the menu item from the page item
        $menu_item = $page_item ? array_merge($menu_item, $page_item) : [];

        // if there are children inside menu item, recursively find matching page items.
        if(array_has($menu_item,'children')){
            $menu_item['children'] = map_pages_to_menu($pages, $menu_item['children']);
        }

        return $menu_item;
    })
    // to remove empty items & reset index
    ->filter()->values()
    ->toArray();
}

$menu = '[
    {"id":1},
    {"id":2},
    {"id":3,"children":[
        {"id":4}
    ]},
    {"id":5,"children":[
        {"id":6},
        {"id":7,"children":[
            {"id":8},
            {"id":9}
        ]}
    ]},
    {"id":10}
]';

$menu = json_decode($menu, true);

$pages = [
    ['id'=>1,'label'=>'About Us','url_path'=>'/about-us'],
    ['id'=>2,'label'=>'Meet the Team','url_path'=>'/meet-the-team'],
    ['id'=>3,'label'=>'Services','url_path'=>'/services'],
    ['id'=>4,'label'=>'Contact Us','url_path'=>'/contact-us'],
    ['id'=>5,'label'=>'Company Mission','url_path'=>'/company-mission'],
    ['id'=>6,'label'=>'History','url_path'=>'/history']
];

$menu = map_pages_to_menu($pages, $menu);

echo json_encode($menu);

摘要:https://implode.io/yTmrDO