> ## Content Index
> Fetch the complete content index at: https://www.karls.io/llms.txt
> Use this file to discover other available public pages before exploring further.

# PHP find array key recursively
- URL: https://www.karls.io/php-find-array-key-recursivelly/
- Published: 2020-02-14T18:53:25.000Z
- Updated: 2025-12-16T13:29:52.000Z
- Author: Karl Scerbiakas
- Tags: Php

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.

```php
<php

function recursiveFind(array $haystack, $needle)
{
    $iterator  = new RecursiveArrayIterator($haystack);
    $recursive = new RecursiveIteratorIterator(
        $iterator,
        RecursiveIteratorIterator::SELF_FIRST
    );
    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.

## Join Devs Who Are Building Better Backend Systems

Get production-tested NestJS patterns, microservices insights,   
and GCP Cost hacks. 

Subscribe 

Email sent! Check your inbox to complete your signup. 

No spam. Unsubscribe anytime.