I've been working on a revision to a hierarchical search and now have to go down more than 3 levels. Issue is I'm having to convert a single level array to a multi-level. I've attempted to use array_search + array_column together but my array is not counted numerically. I need to add children to their parent matching them from their parent array key. I'll share the structures and the expected result.
Here is the current array (main) structure:
$menu = array (
207705 =>
array (
'ID' => 207705,
'title' => 'Home',
'url' => '/',
'description' => '',
'children' =>
array (
),
),
207795 =>
array (
'ID' => 207795,
'title' => 'Category',
'url' => '/',
'description' => 'Lorem ipsum',
'children' =>
array (
207892 =>
array (
'ID' => 207892,
'title' => 'Subcategory0',
'url' => '/',
'description' => '',
),
207791 =>
array (
'ID' => 207791,
'title' => 'Subcategory1',
'url' => '/',
'description' => '',
),
),
),
)
Here is the array of the children I need to merge into main under the corresponding ID:
$children = array (
207892 =>
array (
0 =>
array (
'ID' => 207789,
'title' => 'SubSubCat1',
'url' => '',
),
1 =>
array (
'ID' => 207786,
'title' => 'SubSubCat2',
'url' => '',
),
2 =>
array (
'ID' => 207788,
'title' => 'SubSubCat3',
'url' => '',
),
3 =>
array (
'ID' => 207787,
'title' => 'SubSubCat4',
'url' => '',
),
),
)
The final result I want is:
array (
207705 =>
array (
'ID' => 207705,
'title' => 'Home',
'url' => '/',
'description' => '',
'children' =>
array (
),
),
207795 =>
array (
'ID' => 207795,
'title' => 'Category',
'url' => '/',
'description' => 'Lorem ipsum',
'children' =>
array (
207892 =>
array (
'ID' => 207892,
'title' => 'Subcategory0',
'url' => '/',
'description' => '',
'children' => array (
0 =>
array (
'ID' => 207789,
'title' => 'SubSubCat1',
'url' => '',
),
1 =>
array (
'ID' => 207786,
'title' => 'SubSubCat2',
'url' => '',
),
2 =>
array (
'ID' => 207788,
'title' => 'SubSubCat3',
'url' => '',
),
3 =>
array (
'ID' => 207787,
'title' => 'SubSubCat4',
'url' => '',
),
),
),
207791 =>
array (
'ID' => 207791,
'title' => 'Subcategory1',
'url' => '/',
'description' => '',
),
),
),
)
So far, I've tried many solutions from this thread. Everything either returns false, null, or only the first level of array keys. Is there a function or even a one-liner I can use?
My current code is this:
foreach($children as $id => $item) {
$parent = array_search($id, $menu);
if($show == true) {
//In this case looking to add the current item array to the ['children'] of the found matching element in the $menu array
$search = array_map(function($v){return $v['ID'];},$menu);
$menu[$search] = $item;
}
}
I've been working on a revision to a hierarchical search and now have to go down more than 3 levels. Issue is I'm having to convert a single level array to a multi-level. I've attempted to use array_search + array_column together but my array is not counted numerically. I need to add children to their parent matching them from their parent array key. I'll share the structures and the expected result.
Here is the current array (main) structure:
$menu = array (
207705 =>
array (
'ID' => 207705,
'title' => 'Home',
'url' => 'https://example/',
'description' => '',
'children' =>
array (
),
),
207795 =>
array (
'ID' => 207795,
'title' => 'Category',
'url' => 'https://example/cat/',
'description' => 'Lorem ipsum',
'children' =>
array (
207892 =>
array (
'ID' => 207892,
'title' => 'Subcategory0',
'url' => 'https://example/cat/subcat0/',
'description' => '',
),
207791 =>
array (
'ID' => 207791,
'title' => 'Subcategory1',
'url' => 'https://example/cat/subcat1/',
'description' => '',
),
),
),
)
Here is the array of the children I need to merge into main under the corresponding ID:
$children = array (
207892 =>
array (
0 =>
array (
'ID' => 207789,
'title' => 'SubSubCat1',
'url' => 'https://example/cat/subcat0/sub1',
),
1 =>
array (
'ID' => 207786,
'title' => 'SubSubCat2',
'url' => 'https://example/cat/subcat0/sub2',
),
2 =>
array (
'ID' => 207788,
'title' => 'SubSubCat3',
'url' => 'https://example/cat/subcat0/sub3',
),
3 =>
array (
'ID' => 207787,
'title' => 'SubSubCat4',
'url' => 'https://example/cat/subcat0/sub4',
),
),
)
The final result I want is:
array (
207705 =>
array (
'ID' => 207705,
'title' => 'Home',
'url' => 'https://example/',
'description' => '',
'children' =>
array (
),
),
207795 =>
array (
'ID' => 207795,
'title' => 'Category',
'url' => 'https://example/cat/',
'description' => 'Lorem ipsum',
'children' =>
array (
207892 =>
array (
'ID' => 207892,
'title' => 'Subcategory0',
'url' => 'https://example/cat/subcat0/',
'description' => '',
'children' => array (
0 =>
array (
'ID' => 207789,
'title' => 'SubSubCat1',
'url' => 'https://example/cat/subcat0/sub1',
),
1 =>
array (
'ID' => 207786,
'title' => 'SubSubCat2',
'url' => 'https://example/cat/subcat0/sub2',
),
2 =>
array (
'ID' => 207788,
'title' => 'SubSubCat3',
'url' => 'https://example/cat/subcat0/sub3',
),
3 =>
array (
'ID' => 207787,
'title' => 'SubSubCat4',
'url' => 'https://example/cat/subcat0/sub4',
),
),
),
207791 =>
array (
'ID' => 207791,
'title' => 'Subcategory1',
'url' => 'https://example/cat/subcat1/',
'description' => '',
),
),
),
)
So far, I've tried many solutions from this thread. Everything either returns false, null, or only the first level of array keys. Is there a function or even a one-liner I can use?
My current code is this:
foreach($children as $id => $item) {
$parent = array_search($id, $menu);
if($show == true) {
//In this case looking to add the current item array to the ['children'] of the found matching element in the $menu array
$search = array_map(function($v){return $v['ID'];},$menu);
$menu[$search] = $item;
}
}
Share
Improve this question
edited Feb 17 at 20:05
ADyson
62k16 gold badges75 silver badges89 bronze badges
Recognized by PHP Collective
asked Feb 17 at 19:22
Alexandra ChavezAlexandra Chavez
534 bronze badges
1 Answer
Reset to default 1Something like this should work:
foreach ($menu as $menuID => $menuItem) {
foreach ($menuItem['children'] as $childID => $childItem) {
if (isset($children[$childID])) {
$menu[$menuID]['children'][$childID]['children'] = $children[$childID];
}
}
}