<?php
$array = ["farm"=>
[
"horse"=>
[
"rabbit"=>
[
"name"=> "alan",
"age"=> "21",
"size"=> "166",
"gender"=> "f"
],
]
]
];
function createList($array, $keySearch, $path=null) {
$result = [];
foreach ($array as $key => $item) {
$basePath = $path === null ? $key : $path. "/" . $key;
if (stripos($key, $keySearch) !== false && is_array($item)){
$result[] = ['key' => $key, 'basePath' => $basePath];
}
if(is_array($item))
$result = array_merge($result, createList($item, $keySearch, $basePath));
}
return $result;
}
$keySearch = 'a';
$res = createList($array, $keySearch);
echo '<pre>';
print_r($res);
- Output for 5.5.0 - 5.5.35, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.6 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
- <pre>Array
(
[0] => Array
(
[key] => farm
[basePath] => farm
)
[1] => Array
(
[key] => rabbit
[basePath] => farm/horse/rabbit
)
)
preferences:
184.77 ms | 404 KiB | 211 Q