PHP find array key recursively

PHP find array key recursively

Today I had an issue where I needed to find a key in multidimensional array. It took trial and error ant some googling around, but this is what I found.


function recursiveFind(array $haystack, $needle)
    $iterator  = new RecursiveArrayIterator($haystack);
    $recursive = new RecursiveIteratorIterator(
    foreach ($recursive as $key => $value) {
        if ($key === $needle) {
            return $value;

Probably will come handy in the future. And no recursive functions calling itself etc. Lopping array over arrays is handled by php.

Have a good day!