PHP array_search Function is an inbuilt function in PHP which searches a value in an array. It returns the corresponding key to the value. However, if the value is present more than once then it only returns the first occurrence. In this article, we will discuss the PHP array_search Function along with some examples.
array_search($needle, $haystack, $strictParameter = false)
PHP array_search Function has three parameters. Out of the three, two parameters are mandatory while the third one is optional. Here is a description of all the parameters:
- $needle: The $needle is the first parameter to PHP array_search Function. It specifies the value to search in the array.
- $haystack: The $haystack is the second parameter and specifies the array in which to search into.
- $strictParameter: The third parameter is an optional parameter. It can be either set to TRUE or FALSE and specifies the strictness in search. Also, the default value of this parameter is FALSE. If it is set to TRUE then it checks for identity along with value and vice versa. For example, an integer 10 is not equal to a string 10, if the value is TRUE. You can also read about this on PHP Comparison Operators.
The PHP array_search Function the key of the value on finding it in the array. However, it returns FALSE if the value doesn’t exist. Moreover, if the value is present more than once, then it returns only the first occurrence of the value.
Let’s go through a few examples to demonstrate the working of PHP array_search Function.
Example 1: Basic Usage
<?php $testArray = array('a' => 1, 'b' => 2, 'c' =>3); $pos = array_search(2, $testArray); var_dump($pos); //string(1) "b" ?>
In the above example, we have an array containing key-value pairs. We search for value 2 in the array and PHP array_search Function returns it’s corresponding key ie. b.
Example 2: Default Strict Parameter
<?php $testArray = array('a' => '1', 'b' => '2', 'c' =>'3'); $pos = array_search(2, $testArray); var_dump($pos); //string(1) "b" ?>
We have made a slight change in Example 1. Instead of having integer values, now we have string values in the array.
Example 3: Strict Parameter true
<?php $testArray = array('a' => '1', 'b' => '2', 'c' =>'3', 'd' => 2); $pos = array_search(2, $testArray, true); var_dump($pos); //string(1) "d" ?>
We add another value in the array, this time an integer 2. Also, we pass the strict parameter as true. The function will also validate the data types of parameters. Now, the value of at key ‘b’ is a string 2 and that at
Example 4: Repeated Values
<?php $testArray = array('Monica', 'Chandler', 'Phoebe', 'Ross', 'Rachael', 'Chandler', 'Monica', 'Rachael'); $posRachael = array_search('Rachael', $testArray); var_dump($posRachael); //Rachel is first found at index 4 /* int(4) */ ?>
In the example above, the word Rachael is repeated in the array. However, function returns the first occurrence of the word.
Example 5: Value Not Found
<?php $testArray = array('Monica', 'Chandler', 'Phoebe', 'Ross', 'Rachael', 'Chandler', 'Monica', 'Rachael'); $posRandom = array_search('Random', $testArray); var_dump($posRandom); //Random does not exist in the array and hence array_search returns false /* bool(false) */ ?>
Now, in the above array ‘Random’ does not exist in the array, the function returns false in this case.
We discussed the PHP array_search Function in this article. It is very useful in searching for a particular value in an array. To read more about it, kindly refer to the Official Documentation.
Also, you can go through more PHP Array Functions on Concatly.
Vishesh is currently working as an Intermediate Software Engineer with Orion Health, New Zealand. He graduated with a Masters in Information Technology from the University of Auckland in 2021. With more than 4 years of work experience, his expertise includes Java, Python, Machine Learning, PHP, Databases, Design and Architecture.