Definition and usage
The array_search() function is the same as in_array(), searching for a key value in the array. If the value is found, the key of the matching element is returned. If not found, returns false.
Prior to PHP 4.2.0, functions returned null instead of false on failure.
If the third parameter strict is specified as true, the key name of the corresponding element will only be returned if the data type and value are consistent.
Grammar
array_search(value,array,strict)
Copy after login
Parameters |
Description |
value |
Required. Specifies the value to search for in the array. |
array |
Required. The array to be searched. |
strict |
参数 |
描述 |
value |
必需。规定在数组中搜索的值。 |
array |
必需。被搜索的数组。 |
strict |
可选。可能的值:
如果值设置为 true,还将在数组中检查给定值的类型。(参见例子 2)
|
Optional. Possible values:
If the value is set to true, the type of the given value will also be checked in the array. (See Example 2)
|
Example #1 array_search() ExampleCopy code
The code is as follows:
$array = array(0 => 'blue', 1 => 'red', 2 => 'green ', 3 => 'red');
$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $ array); // $key = 1;
?>
Warning
This function may return the Boolean value FALSE, but it may also return a value equal to FALSE Non-Boolean value, such as 0 or "". See the Boolean Types chapter for more information. The === operator should be used to test the return value of this function.
Example 1
Copy code
The code is as follows:
$a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse");
echo array_search("Dog",$a);
?>
Output: a
Example 2
Copy code
The code is as follows:
$a=array("a"=>"5","b"=>5,"c"=>"5");
echo array_search(5,$a,true);
?>
Output:
b
http://www.bkjia.com/PHPjc/321549.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321549.htmlTechArticle
Definition and usage The array_search() function is the same as in_array(), searching for a key value in the array. If the value is found, the key of the matching element is returned. If not found, return...