PHP multidimensional array search by value - GeeksforGeeks

17 Dec, 2021
In PHP, multidimensional array search refers to searching a value in a multilevel nested array. There are various techniques to carry out this type of search, such as iterating over nested arrays, recursive approaches and inbuilt array search functions.
Iterative Approach:Iterating over the array and searching for significant match is the simplest approach one can follow. Check if an element of the given array is itself an array or not and add the element to the search path, else run array search on the nested array.
Example:
<?php// PHP program to carry out multidimensional array search    // Function to iteratively search for a given valuefunction searchForId($search_value, $array, $id_path) {      // Iterating over main array    foreach ($array as $key1 => $val1) {          $temp_path = $id_path;                  // Adding current key to search path        array_push($temp_path, $key1);          // Check if this value is an array        // with atleast one element        if(is_array($val1) and count($val1)) {              // Iterating over the nested array            foreach ($val1 as $key2 => $val2) {                  if($val2 == $search_value) {                                              // Adding current key to search path                    array_push($temp_path, $key2);                                              return join(" --> ", $temp_path);                }            }        }                  elseif($val1 == $search_value) {            return join(" --> ", $temp_path);        }    }          return null;}  // Multidimensional array $gfg_array = array(    array(        'score' => '100',        'name' => 'Sam',        'subject' => 'Data Structures'    ),    array(        'score' => '50',        'name' => 'Tanya',        'subject' => 'Advanced Algorithms'    ),    array(        'score' => '75',        'name' => 'Jack',        'subject' => 'Distributed Computing'    ));  $search_path = searchForId('Advanced Algorithms',                    $gfg_array, array('$'));  print($search_path);  ?>
$ --> 1 --> subject

Recursive Approach:In case, when levels of nested arrays increase, it becomes hard to write such programs and debug them. In such cases its better to write a recursive program which can cleanly be written without adding any nested for loops.
Example:
<?php// PHP program to carry out multidimensional array search    // Function to recursively search for a given valuefunction array_search_id($search_value, $array, $id_path) {          if(is_array($array) && count($array) > 0) {                  foreach($array as $key => $value) {              $temp_path = $id_path;                          // Adding current key to search path            array_push($temp_path, $key);              // Check if this value is an array            // with atleast one element            if(is_array($value) && count($value) > 0) {                $res_path = array_search_id(                        $search_value, $value, $temp_path);                  if ($res_path != null) {                    return $res_path;                }            }            else if($value == $search_value) {                return join(" --> ", $temp_path);            }        }    }          return null;}  // Multidimensional (Three dimensional) array$gfg_array = array(    "school1" => array(        "year" => "2017",        "data" => array(            'score' => '100',            'name' => 'Sam',            'subject' => 'Data Structures'        )    ),          "school2" => array(        "year" => "2018",        "data" => array(            'score' => '50',            'name' => 'Tanya',            'subject' => 'Advanced Algorithms'        )    ),          "school3" => array(        "year" => "2018",        "data" => array(            'score' => '75',            'name' => 'Jack',            'subject' => 'Distributed Computing'        )    ));  $search_path = array_search_id('Jack', $gfg_array, array('$'));print($search_path);  ?>
$ --> school3 --> data --> name

Multidimensional array search using array_search() method:The array_search() is an inbuilt function which searches for a given value related to the given array column/key. This function only returns the key index instead of a search path. The array_column() function returns the values from a single column in the input array.
Example:
<?php// PHP program to carry out multidimensional array search    // Multidimensional array$gfg_array = array(    array(        'score'   => '100',        'name'    => 'Sam',        'subject' => 'Data Structures'    ),    array(        'score'   => '50',        'name'    => 'Tanya',        'subject' => 'Advanced Algorithms'    ),    array(        'score'   => '75',        'name'    => 'Jack',        'subject' => 'Distributed Computing'    ));  $id = array_search('50', array_column($gfg_array, 'score'));echo $id;  ?>
1

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.
Picked
PHP
PHP Programs
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Insert Form Data into Database using PHP ?
How to convert array to string in PHP ?
How to receive JSON POST with PHP ?
PHP | Converting string to Date and DateTime
Download file from URL using 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 fetch data from localserver database and display on HTML table using PHP ?
Split a comma delimited string into an array in PHP