How to search by key=>value in a multidimensional array in PHP ? (original) (raw)
Last Updated : 15 Jul, 2024
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.
Table of Content
**Recursive Approach:
Check if the key exists in a multidimensional array and if the value of a key is equal to the required one then the result is stored in an array and recurs through each element.
**Example:
Program to search students named "AMIT" in a multidimensional array and print results.
php `
value // Function to recursively search for a // given key=>value function search($array, key,key, 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[array[array[key] == $value) { results[]=results[] = results[]=array; } // Iterate for each element in array foreach ($array as $subarray) { // recur through each element and append result results=arraymerge(results = array_merge(results=arraymerge(results, search($subarray, key,key, 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(res = search(res=search(arr, 'name', 'AMIT'); foreach ($res as $var) { echo var["rollNo"]."−".var["rollNo"]." - ".var["rollNo"]."−".var['name'] . ""; } ?>
`
Output
101 - AMIT202 - AMIT
**Iterative Approach:
The iterative approach uses a foreach loop to traverse the multidimensional array. For each element, it checks if the specified key exists and matches the target value. If matched, the element is added to the result array. This method is simple and effective.
**Example:
Program to search students who lives in "Delhi" in a multidimensional array and print result.
php `
value // Function to iteratively search for a // given key=>value function search($array, key,key, key,value) { // RecursiveArrayIterator to traverse an // unknown amount of sub arrays within // the outer array. arrIt=newRecursiveArrayIterator(arrIt = new RecursiveArrayIterator(arrIt=newRecursiveArrayIterator(array); // RecursiveIteratorIterator used to iterate // through recursive iterators it=newRecursiveIteratorIterator(it = new RecursiveIteratorIterator(it=newRecursiveIteratorIterator(arrIt); foreach ($it as $sub) { // Current active sub iterator subArray=subArray = subArray=it->getSubIterator(); if ($subArray[$key] === $value) { result[]=iteratortoarray(result[] = iterator_to_array(result[]=iteratortoarray(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(res = search(res=search(arr, 'location', 'Delhi'); foreach ($res as $var) { echo var["name"]."−".var["name"]." - ".var["name"]."−".var['location'] .""; } ?>
`
Output
BOB - DelhiAMIT - DelhiANMOL - DelhiTONNY - Delhi
Using array_filter() Function
The array_filter() function in PHP can be used to search for a key-value pair in a multidimensional array by providing a custom callback function. The callback checks each subarray for the specified key and value, filtering out non-matching elements.
**Example
PHP `
1, "name" => "John"), array("id" => 2, "name" => "Jane"), array("id" => 3, "name" => "Doe") ); $key = "id"; $value = 2; result=arrayfilter(result = array_filter(result=arrayfilter(array, function($subarray) use ($key, $value) { return isset($subarray[$key]) && subarray[subarray[subarray[key] == $value; }); if (!empty($result)) { print_r(array_shift($result)); } else { echo "No match found."; } ?>`
Output
Array ( [id] => 2 [name] => Jane )