Home >Backend Development >PHP Tutorial >php-Arrays function-array_search-finds a given value in an array and returns the corresponding key name_PHP tutorial
array_search() finds the given value in the array and returns the corresponding key name
【Function】
This function will search for a given value in the specified array and return the key name of the value if found, otherwise it will return false.
【Scope of use】
php4>=4.0.5, php5.
【Use】
mixed array_search( mixed needle,array haystack[,bool strict] )
needle/required/value to be found
haystack/required/original array of the upcoming search operation
strict/optional/true, the function will also check the type of needle in haystack
If needle appearing more than once in haystack, return the first matching construct
【Example】
[php]
$array=array('blue','red','green','red');
echo $key=array_search('green',$array);
echo "n";
echo $key=array_search('red',$array,false);
/*
2
1
*/
Excerpted from zuodefeng’s notes