Home >>PHP Array Functions >PHP array_search() Function

PHP array_search() Function

PHP array_search() Function

PHP array_search() function is used to search for a particular value in the given input array and if that value is found in the array then it returns its corresponding key. If there are more than one values given then the key of first matching value will be returned as output.

Syntax:

array_search($value, $array, $strict);

Parameter Values

Parameter Description
value This is a required parameter. This parameter defines the value to search for.
array This is a required parameter. This parameter defines the array to search in.
strict This is an optional parameter. If this parameter is set to TRUE, then this function will search for identical elements in the array.

Here is an example of array_search() function in PHP:

<html>
<body>
<?php
$x=array("Audi","BMW","Jaguar","Lamborghini");
echo array_search("BMW",$x);
?>
</body>
</html>
Output:
1

Example 2:

<html>
<body>
<?php
$x=array("a"=>"2","b"=>2,"c"=>"5","d"=>"8","e"=>8);
echo array_search(2,$x);
echo "<br>";
echo array_search(2,$x,true);
?>
</body>
</html>
Output:
a
b

No Sidebar ads