<?php
function recursiveFind(array $array, $needle) {
$iterator = new RecursiveArrayIterator($array);
$recursive = new RecursiveIteratorIterator(
$iterator,
RecursiveIteratorIterator::SELF_FIRST
);
$return = [];
foreach ($recursive as $key => $value) {
if ($key === $needle) {
$return[] = $value;
}
}
return $return;
}
$array = [
'foo' => 'one',
'bar' => [
'boo' => 'two',
'foo' => 'three',
'loo' => [
'foo' => 'four',
],
],
'goo' => [
'foo' => 'five',
]
];
var_export(recursiveFind($array, 'foo'));
- Output for 7.4.33, 8.0.1 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
- array (
0 => 'one',
1 => 'three',
2 => 'four',
3 => 'five',
)
preferences:
98.62 ms | 402 KiB | 90 Q