Home > Backend Development > PHP Tutorial > How to Efficiently Search for Key-Value Pairs in PHP's Multidimensional Arrays?

How to Efficiently Search for Key-Value Pairs in PHP's Multidimensional Arrays?

Susan Sarandon
Release: 2024-12-28 15:47:33
Original
189 people have browsed it

How to Efficiently Search for Key-Value Pairs in PHP's Multidimensional Arrays?

Searching for Key-Value Pairs in Multidimensional Arrays in PHP

Introduction:

Finding specific subarrays based on key-value matches within multidimensional arrays can be challenging, especially when the array's depth is unknown. This article explores a recursive approach to efficiently accomplish this search operation.

Function Definition:

function search($array, $key, $value)
{
    // Initialize an empty results array
    $results = array();

    // Check if the current element is an array
    if (is_array($array)) {
        // If the current element matches the key-value pair, add it to the results
        if (isset($array[$key]) && $array[$key] == $value) {
            $results[] = $array;
        }

        // Recursively search through each subarray
        foreach ($array as $subarray) {
            $results = array_merge($results, search($subarray, $key, $value));
        }
    }

    // Return the accumulated results
    return $results;
}
Copy after login

Example:

Consider the following multidimensional array:

$arr = array(
    0 => array('id' => 1, 'name' => "cat 1"),
    1 => array('id' => 2, 'name' => "cat 2"),
    2 => array('id' => 3, 'name' => "cat 1")
);
Copy after login

When searching for the key-value pair 'name' => 'cat 1', the function will return:

array(
    0 => array('id' => 1, 'name' => "cat 1"),
    1 => array('id' => 3, 'name' => "cat 1")
);
Copy after login

Performance Considerations:

For scenarios where efficiency is paramount, an alternative approach is available:

function searchOptimized($array, $key, $value)
{
    $results = array();
    searchOptimized_r($array, $key, $value, $results);
    return $results;
}

function searchOptimized_r($array, $key, $value, &$results)
{
    if (!is_array($array)) {
        return;
    }

    if (isset($array[$key]) && $array[$key] == $value) {
        $results[] = $array;
    }

    foreach ($array as $subarray) {
        searchOptimized_r($subarray, $key, $value, $results);
    }
}
Copy after login

In this optimized version, the search results are accumulated directly in the temporary $results array, eliminating the need for array merging. By specifying the pass-by-reference in the call to searchOptimized_r, this version maintains high performance while preserving compatibility with older PHP versions.

The above is the detailed content of How to Efficiently Search for Key-Value Pairs in PHP's Multidimensional Arrays?. 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