function recursiveFind(array $haystack, $needle) { $iterator = new RecursiveArrayIterator($haystack); $recursive = new RecursiveIteratorIterator( $iterator, RecursiveIteratorIterator::SELF_FIRST ); foreach ($recursive as $key => $value) { if ($key === $needle) { yield $value; } } } // Usage foreach (recursiveFind($haystack, $needle) as $value) { // Use `$value` here }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter