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);

Parameter,

$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 :

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

echo "<br>Look at given array<br>";
print_r($array);

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

$identical=array("a"=>"1","b"=>2,"c"=>"4","d"=>4,"e"=>5);
echo "<br><br>Searched value for given array where strict set to true.<br>";
print_r($identical);
$key= array_search(4,$identical,TRUE);
echo "<br>Key for searched value 4 : ".$key;
?>

Output :

Comments

Leave a Reply

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

41396