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

20 May, 2019
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.
<?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>";}  ?>
101 - AMIT202 - 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.
<?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>";}  ?>
BOB - DelhiAMIT - DelhiANMOL - DelhiTONNY - Delhi

PHP-array
Picked
PHP
PHP Programs
Web Technologies
Web technologies Questions
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Insert Form Data into Database using PHP ?
How to convert array to string in PHP ?
PHP | Converting string to Date and DateTime
How to receive JSON POST with PHP ?
How to pass JavaScript variables to PHP ?
How to Insert Form Data into Database using PHP ?
How to convert array to string in PHP ?
How to call PHP function on the click of a Button ?
How to pass JavaScript variables to PHP ?
Split a comma delimited string into an array in PHP