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

Syntax:

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

Parameters of array_search()

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

$haystack:
The second parameter is required. It refers to the original array, which needs to be searched.

$strict:
The third parameter is optional. It can be set to TRUE or FALSE, and refers to the strictness of search. The default value of this parameter is FALSE.
When it is set to true, the string 45 is not same as number 45.

Return Values:
Returns the key of a value if it is found in the array, and FALSE otherwise. 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

The above example will output:

Example #2 – With third parameter “strict” mode

The above example will output:

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

The above example will output:

LEAVE A REPLY

Please enter your comment!
Please enter your name here