PHP array_search Function | Search Value in Array

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.

Syntax

array_search($needle, $haystack, $strictParameter = false)

Parameters

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.

Return Value

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.

Examples

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. Since, strict parameter is not passed in array_search function, it’s default value will be false. As as result, PHP array_search Function does not validate the data types of the values.

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 value at ‘d‘ is an integer 2. Hence, the function returns key ‘d‘ as it matches the data type also.

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.

Conclusion

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.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *