$array = ["farm"=>
[
"horse"=>
[
"rabbit"=>
[
"fred1"=> "fred1",
"fred2"=> "fred2",
"fred3"=> "fred3",
"fred4"=> "fred4"
],
"raccoon"=>
["frida"=> "frida"]
]
]
];
I want to create an array from my for each loop:
$keySearch = "o";
function createList($array, $keySearch, $path) {
foreach ($array as $key => $item) {
$basePath = $path === null ? $key : $path. "/" . $key;
if(is_array($item)){
if (stripos($key, $keySearch) !== false){
$a['key'] = $key ;
$b['basePath'] = $basePath;
$result[] = array_merge_recursive ($a, $b);
}
createList($item, $keySearch, $basePath);
}
}
print_r($result);
}
createList($array, $keySearch, '');
My result is:
Array
(
[0] => Array
(
[key] => horse
[basePath] => farm/horse
)
)
Array
(
[0] => Array
(
[key] => raccoon
[basePath] => farm/horse/raccoon
)
)
What I actually expect is:
Array
(
[0] => Array
(
[key] => horse
[basePath] => farm/horse
)
[1] => Array
(
[key] => raccoon
[basePath] => farm/horse/raccoon
)
)
$keySearch = "o"