PHP array_search() Function

The array_search() function is used to search the array against the given value. The function returns the first corresponding key if successful.

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.

ParameterDescription
valueRequired. Specifies the value to search for
arrayRequired. Specifies the array to search in
strictOptional. If this parameter is set to TRUE, then this function will search for identical elements in the array. Possible values:truefalse – DefaultWhen set to true, the number 5 is not the same as the string 5 (See example 2)

Syntax

 array_search(value,array,strict) 

Example

$array = array("a"=>"Cooper","b"=>5,"c"=>"6");
echo array_search("Cooper",$array,true);

Output

a

Leave a Reply

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