How to search by key=>value in a multidimensional array in PHP ?

In PHP, multidimensional array search refers to searching a key=>value in a multilevel nested array. This search can be done either by the iterative or recursive approach.

Recursive Approach: Check if the key exists in a multidimensional array and the value of a key is equal to required one then the result stored in an array and also recur through each element.

Example: Program to search students whose name is “AMIT” in a multidimensional array and print result.



filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to carry out multidimensional array
// search by key=>value
  
// Function to recursively search for a
// given key=>value 
function search($array, $key, $value) {
    $results = array();
      
    // if it is array
    if (is_array($array)) {
          
        // if array has required key and value
        // matched store result 
        if (isset($array[$key]) && $array[$key] == $value) {
            $results[] = $array;
        }
          
        // Iterate for each element in array
        foreach ($array as $subarray) {
              
            // recur through each element and append result 
            $results = array_merge($results
                    search($subarray, $key, $value));
        }
    }
  
    return $results;
}
  
// Multidimensional array for student list
$arr = array(
    "A" => array(
        1 => array('rollNo'=>101, 'name'=>"AMIT"),
        2 => array('rollNo'=>102, 'name'=>"BHUWAN"),
        3 => array('rollNo'=>103, 'name'=>"BOB"),
        4 => array('rollNo'=>104, 'name'=>"CAROT")
    ),
    "B" => array(
        1 => array('rollNo'=>201, 'name'=>"ABHISHEK"),
        2 => array('rollNo'=>202, 'name'=>"AMIT"),
        3 => array('rollNo'=>203, 'name'=>"RONNY"),
        4 => array('rollNo'=>204, 'name'=>"LOBO")
    ),
    "C" => array(
        1 => array('rollNo'=>301, 'name'=>"ANMOL"),
        2 => array('rollNo'=>302, 'name'=>"TONNY"),
        3 => array('rollNo'=>303, 'name'=>"SANJI")
    )
);
  
$res = search($arr, 'name', 'AMIT');
  
foreach ($res as $var) {
    echo $var["rollNo"]." - ".$var['name'] . "<br>";
}
  
?>

chevron_right


Output:

101 - AMIT
202 - AMIT

Iterative Approach: Below implementation is the iterative approach

Example: Program to search students who lives in “Delhi” in a multidimensional array and print result.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to carry out multidimensional
// array search by key=>value
   
// Function to iteratively search for a
// given key=>value  
function search($array, $key, $value) {
   
    // RecursiveArrayIterator to traverse an
    // unknown amount of sub arrays within
    // the outer array.
    $arrIt = new RecursiveArrayIterator($array);
   
    // RecursiveIteratorIterator used to iterate
    // through recursive iterators
    $it = new RecursiveIteratorIterator($arrIt);
   
    foreach ($it as $sub) {
   
        // Current active sub iterator
        $subArray = $it->getSubIterator();
   
        if ($subArray[$key] === $value) {
            $result[] = iterator_to_array($subArray);
         }
    }
    return $result;
}
   
// Multidimensional array for students list
$arr = array(
    "A" => array(
        1 => array('name'=>"Alice", 'location'=>"Dehradun"),
        2 => array('name'=>"BHUWAN", 'location'=>"Mumbai"),
        3 => array('name'=>"BOB", 'location'=>"Delhi"),
        4 => array('name'=>"CAROT", 'location'=>"Haryana")
    ),
    "B" => array(
        1 => array('name'=>"ABHISHEK", 'location'=>"Dehradun"),
        2 => array('name'=>"AMIT", 'location'=>"Delhi"),
        3 => array('name'=>"RONNY", 'location'=>"Bengaluru"),
        4 => array('name'=>"LOBO", 'location'=>"Pune")
    ),
    "C" => array(
        1 => array('name'=>"ANMOL", 'location'=>"Delhi"),
        2 => array('name'=>"TONNY", 'location'=>"Delhi"),
        3 => array('name'=>"SANJI", 'location'=>"Haryana")
    )
);
   
$res = search($arr, 'location', 'Delhi');
   
foreach ($res as $var) {
    echo $var["name"]." - ".$var['location'] ."<br>";
}
  
?>

chevron_right


Output:

BOB - Delhi
AMIT - Delhi
ANMOL - Delhi
TONNY - Delhi


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.