Been trying to solve this with help of Laravel collections or array helpers in most efficient way. Can anyone help with this?
$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'],
...
...
];
The ID key from nested array $menu needs to search through $pages array based on same ID and then merge this whole row back to $menu.
My Function:
$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();
How do I add the array data from $pages to "children" with recursive iteration? And is there better way to make this function more efficient? I want to make use of Laravel collections or array helpers to solve this instead of using foreach loop, etc.
Below is the expected result:
$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, ...
...
];
Can anyone show me? Thanks!