How can I find the value of the first matching key in a multidimensional array when recursion is involved?

Linda Hamilton
Release: 2024-10-30 11:43:27
Original
190 people have browsed it

How can I find the value of the first matching key in a multidimensional array when recursion is involved?

Retrieving the Value of the First Matching Key in a Multidimensional Array: Tackling Recursion Woes

In software development, navigating multidimensional arrays and searching for specific keys is a common task. However, when recursion is involved, things can get tricky. Let's dissect the following code snippet that aims to find the value associated with a matching key:

<code class="php">private function find($needle, $haystack) {
    foreach ($haystack as $name => $file) {
        if ($needle == $name) {
            return $file;
        } else if(is_array($file)) { //is folder
            return $this->find($needle, $file); //file is the new haystack
        }               
    }
    
    return "did not find";
}</code>
Copy after login

The issue lies within the recursion itself. Upon encountering an array within the haystack, the file variable becomes the new haystack. However, the reference to the original haystack is lost, potentially leading to an eternal recursion cycle.

To address this, consider the following solutions:

RecursiveIteratorIterator

PHP 5.6 and later introduces RecursiveIteratorIterator, which simplifies the task considerably:

<code class="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;
        }
    }
}</code>
Copy after login

This approach takes advantage of a RecursiveArrayIterator that traverses the array and a RecursiveIteratorIterator that efficiently iterates through all elements, including nested arrays.

Generator-Based Function

For PHP 5.6 and newer, you can utilize generators to retrieve all matching values:

<code class="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) {
            yield $value;
        }
    }
}</code>
Copy after login

This function returns matching values using the yield keyword, allowing you to iterate through all of them with a foreach loop.

The above is the detailed content of How can I find the value of the first matching key in a multidimensional array when recursion is involved?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!