The array_search() function search an array for a value and returns the key.
PHP array_search() Function has the following syntax.
array_search(value,array,strict)
Parameter | Is Required | Description |
---|---|---|
value | Required. | Value to search for |
array | Required. | Array to search in |
strict | Optional. | If go strict |
Possible values:
Search an array for an element
<?php
$a=array("a"=>"Java","b"=>"PHP","c"=>"java2s.com");
echo array_search("PHP",$a);
?>
The code above generates the following result.
Search an array for the value 5 and return its key. Pay more attention to the "".
<?php
$a=array("a"=>"5","b"=>5,"c"=>"5");
echo array_search(5,$a,true);
?>
The code above generates the following result.