Navigate back to the homepage

How to find the array index key of multi-dimensional array

Parimal Ghadiyali
January 20th, 2019 · 1 min read

array_search() is a built in PHP function that searches the array for a given value and returns the first corresponding key if successful.

Syntax:

1array_search ( mixed $needle , array $haystack [, bool $strict = FALSE ] ) : mixed

Support: (PHP 4 >= 4.0.5, PHP 5, PHP 7)

$needle:

  • The $needle parameter is required.
  • It refers to the value that needs to be searched in the array.

$haystack:

  • The $haystack parameter is required.
  • It refers to the original array, on which search operation is performed.

$strict:

  • The $haystack parameter is an optional one.
  • It can be set to true or false, and refers to the strictness of search.
  • The default value of this parameter is false.
  • Example: when it is set to true, the string 45 is not same as number 45.

Return Values:

  • The key of a value is returned if it is found in an array, otherwise false.
  • If the value is found in the array more than once, the first matching key is returned.

Example #1 – Search an array for the value “Jerusalem” and return its key

1$records = [
2 'New Delhi' => 'India',
3 'Madrid' => 'Spain',
4 'Israel' => 'Jerusalem',
5 'Amsterdam' => 'Netherlands'
6];
7$countries = array_search("Jerusalem", $records);
8print_r($countries);
The above example will output:
1Israel

Example #2 – With third parameter “strict” mode

1$records = [
2 'x' => '10',
3 'y' => 10,
4 'z' => '10'
5];
6echo 'Without strict parameter: ';
7echo array_search(10, $records);
8echo 'With strict parameter: ';
9echo array_search(10, $records, true);
The above example will output:
1Israel

Example #3 – You don’t have to write your own function to search through a multidimensional array, (example with array_column())

1$records = [
2 [
3 'id' => 12,
4 'country' => 'India',
5 'capital' => 'New Delhi',
6 ],
7 'Spain' => [
8 'id' => 24,
9 'country' => 'Spain',
10 'capital' => 'Madrid',
11 ],
12 [
13 'id' => 91,
14 'country' => 'Israel',
15 'capital' => 'Jerusalem',
16 ],
17 'Netherlands' => [
18 'id' => 121,
19 'country' => 'Netherlands',
20 'capital' => 'Amsterdam',
21 ]
22];
23$countries = array_column($records, 'country');
24print_r($countries);
25$key = array_search('Israel', array_column($records, 'country'));
26echo 'Key: ' . $key;
The above example will output:
1Array
2(
3 [0] => India
4 [1] => Spain
5 [2] => Israel
6 [3] => Netherlands
7)
8Key: 2

Hope you find this helpful

More articles from MeshWorld

How to Get Values from Multi-dimensional Arrays with array_column()

array_column() is an inbuilt PHP function which is used to get the values from a single column from the multi-dimensional array or an array of objects.

November 15th, 2018 · 1 min read

Identify Unused Code Using Chrome DevTools

Chrome DevTools allows developers to optimize using Sources panel to identify unused code on your web page.

November 15th, 2018 · 2 min read
© 2017–2020 MeshWorld
Link to $https://twitter.com/meshworldindiaLink to $https://facebook.com/meshworldindiaLink to $https://instagram.com/meshworld.india/