How to search by key = & gt; value in multidimensional array in PHP?

PHP

Recursive approach:check if the key exists in the multidimensional array and the key value is equal to the required one, and then the result is stored in the array and also iterated through each element.Example:A program to find students named "AMIT" in a multidimensional array and display the results.
// PHP program for executing a multidimensional array
// search by key = > value 
// Function for recursive search // given key = > value function search ( $array , $key , $value ) { $results = array (); // if it's an array if ( is_array ( $array )) { // if the array has the required key and value // matches the result of the store if (isset ( $array [ $key ]) & amp; & amp; $array [ $key ] == $value ) { $results [] = $array ; } // Iterate for each item in the array foreach ( $array as $subarray ) { // repeat each element and add the result $results = array_merge ( $results , search ( $subarray , $key , $value )); } } return $results ; }  
// Multidimensional array for the list of students $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 >" ; }  
?>
Exit:
 101 - AMIT 
202 - AMIT
Iterative approach:below the implementation of the iterative approachExample:program for finding students living in Delhi, in a multidimensional array and displaying the results.
/ / PHP program for multidimensional execution
// search array by key = > value 
// Function for iterative search // given key = > value function search ( $array , $key , $value ) { // RecursiveArrayIterator to pass // unknown number of nested arrays in // external array. $arrIt = new RecursiveArrayIterator ( $array ); // RecursiveIteratorIterator is used to iterate // via recursive iterators

$it = new RecursiveIteratorIterator ( $arrIt ); foreach ( $it as $sub ) {

// Current active iterator $subArray = $it -> getSubIterator(); if ( $subArray [ $key ] === $value ) { $result [] = iterator_to_array ( $subArray ); } } return $result ; }  
// Multidimensional array for the list of students $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 > " ; }  
?>
Exit:

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