PHP array_search() function

PHP array_search() function is used to search the array for a given value and returns the first corresponding key if successful.  PHP array_search() function is PHP built-in function.

Syntax :

array_search(mixed $search , array $array, bool $strict = false);


$search : Required. It is searched value. If searched value is a string, the comparison is done in a case-sensitive manner.

$array : Required. It is input array.

$strict: Optional. Default value is false. If It is TRUE, checks identical type of the value.

Return : It returns the key for searched value if it is found in the array otherwise it returns false. If there are more than one matches, the first matched key will be returned.

Let's see below example to understand php array_search() Function in details.



Example :

$array = array("sap","html","css","java","mysql","bootstrap","asp","oracle","php");

echo "<br>Look at given array<br>";

echo "<br><br>Searched value for given array";
$key= array_search("bootstrap",$array);
echo "<br>Key for searched value 'bootstrap' : ".$key;

echo "<br><br>Searched value for given array where strict set to true.<br>";
$key= array_search(4,$identical,TRUE);
echo "<br>Key for searched value 4 : ".$key;


Leave a Reply

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