How to search a multi dimensional array in PHP?

We know that an array is a built-in data type of PHP that stores a number of values in one single variable.

A multidimensional array is a type of an array that can store one or more than one arrays.

I have discussed about array and different types of array in my previous post which you can see at: https://webtechbased.com/what-is-an-associative-array-in-php/.

Now we will know how to search a multi dimensional array in PHP for a given value.

Search a multi dimensional array in PHP

First of all we will create a multidimensional array and we can view the information of the created array using the var_dump function.

Create a multi dimensional array in PHP

<?php
$shapes = [
	["shape_type"=>"circle", "color"=>"blue"],
        ["shape_type"=>"rectangle", "color"=>"green"],
        ["shape_type"=>"square", "color"=>"red"]
];
var_dump($shapes);
?>

We get the following information.

array(3) {
  [0]=>
  array(2) {
    ["shape_type"]=>
    string(6) "circle"
    ["color"]=>
    string(4) "blue"
  }
  [1]=>
  array(2) {
    ["shape_type"]=>
    string(9) "rectangle"
    ["color"]=>
    string(5) "green"
  }
  [2]=>
  array(2) {
    ["shape_type"]=>
    string(6) "square"
    ["color"]=>
    string(3) "red"
  }
}

Now we can follow any of the following methods to find out whether the shapes array contains the green value and if so, which key it contains.

Methods to search a value in a multi dimensional array

Now we will know these methods one by one.

Method 1: Using array_search/array_keys and array_column PHP functions

We can easily implement this by using array_column and array_search or array_keys PHP functions together.

array_column function returns the values from a single column of the array. We can learn more about array_column function at: https://www.php.net/manual/en/function.array-column.php.

array_search function searches the array for a given value and returns the key for value if it is found in the array, false otherwise. We can learn more about array_search function at: https://www.php.net/manual/en/function.array-search.php.

array_keys function returns the keys from the array. We can learn more about array_keys function at: https://www.php.net/manual/en/function.array-keys.php.

<?php
$shapes = [
    ["shape_type"=>"circle", "color"=>"blue"],
    ["shape_type"=>"rectangle", "color"=>"green"],
    ["shape_type"=>"square", "color"=>"red"]
];

$key = array_search("green", array_column($shapes, "color"));
var_dump($key); // int(1)

$key = array_search("black", array_column($shapes, "color"));
var_dump($key); // bool(false)

$keys = array_keys(array_column($shapes, "color"), "green");
var_dump($keys); // array(1) { [0]=>  int(1) }
?>

Here we used array_column($shapes, “color”) to get the array [0 => blue, 1 => green, 2 => red].

Since the index of green value is 1 in the above array, so If we search with green in array_search function we will get 1 which we can see using the var_dump function.

If we search with black in array_column function we will get false value which we can see using var_dump function.

array_search is useful for one result but for multiple results we can use array_keys. From the above example we can find the information of keys variable for array_keys function is: array(1) { [0]=> int(1) }.

In the case of associative array, if we want to get the key name, we will change the syntax for array_keys and the syntax would be:

$keys = array_keys(array_combine(array_keys($shapes), array_column($shapes, "color")), "green");
Method 2: Using array_filter PHP function

We can search a value in a multi dimensional array by using array_filter PHP function.

array_filter function filters elements of an array by iterating over each value in the array passing them to the callback function.

We can learn more about array_filter function at: https://www.php.net/manual/en/function.array-filter.php

<?php
$shapes = [
    ["shape_type"=>"circle", "color"=>"blue"],
    ["shape_type"=>"rectangle", "color"=>"green"],
    ["shape_type"=>"square", "color"=>"red"]
];

$search = "green";
$filtered = array_filter($shapes,function($v,$k) use ($search){
  return $v['color'] == $search;
},ARRAY_FILTER_USE_BOTH);  
var_dump($filtered); // array(1) {[1]=> array(2) {["shape_type"]=> string(9) "rectangle" ["color"]=> string(5) "green"}}

$keys =  array_keys($filtered); 
var_dump($keys); // array(1) {[0]=> int(1)}
?>

In the above code we first use the array_filter function to get an array that matches the search value (green). So there is only one array in a multi dimensional array. Then we get the keys using the array_keys function.

In this case, if the multi dimensional array is an associative array i.e. if it is like the following array:

$shapes = [
    "circle"=>["shape_type"=>"circle", "color"=>"blue"],
    "rectangle"=>["shape_type"=>"rectangle", "color"=>"green"],
    "square"=>["shape_type"=>"square", "color"=>"red"]
];

Then the output for the var_dump function after executing array_keys function would be as follows:

// array(1) {[0]=>  string(9) "rectangle"}
Method 3: Using custom function

We can search a value in a multi dimensional array by using our own function. Our code will be as follows.

<?php
function arraySearch($array, $index, $value) {
   $keys = [];
   if(!empty($array)){
       foreach ($array as $ind => $val) {
           if ($val[$index] == $value) {
               array_push($keys, $ind);
           }
       }
   }
   return $keys;
}

$shapes = [
    ["shape_type"=>"circle", "color"=>"blue"],
    ["shape_type"=>"rectangle", "color"=>"green"],
    ["shape_type"=>"square", "color"=>"red"]
];

$search = "green";
$keys = arraySearch($shapes, 'color', $search);
var_dump($keys); // array(1) {[0]=> int(1)}
?>

Here I have created a custom function named arraySearch. In the beginning of this function I have created an empty array named $key and then in the multi dimensional array I have used foreach loop to get the inner array. If the value of the key/index (color) of this inner array matches with the search value (green), that index is pushed to the $key array and finally $key is returned.

Then if we call the arraySearch function and if we get the search value in the array then the information of the array can be seen by using the var_dump function.
If the search value is not found in the array then empty array will be returned.

Here I have discussed three methods. You can choose any one of these methods according to your requirement and structure of the multi dimensional array.