3v4l.org

run code in 300+ PHP versions simultaneously
<?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'));

preferences:
32.14 ms | 402 KiB | 5 Q