Home  >  Article  >  Backend Development  >  How can I find the value of the first matching key in a multidimensional array when recursion is involved?

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

Linda Hamilton
Linda HamiltonOriginal
2024-10-30 11:43:27191browse

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>

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>

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>

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!

Statement:
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