PHP array_search() 函数
实例
在数组中搜索键值 "red",并返回它的键名:
<?php $a=array("a"=>"red","b"=>"green","c"=>"blue"); echo array_search("red",$a); ?>
定义和用法
array_search() 函数在数组中搜索某个键值,并返回对应的键名。
Tushirin da labarai
Funksiya array_search() kuma in_array() kowane, a koyi hankomin da keya. wanda kowa, an fahimci kuma koyi keya, an raba kuma an fahimci kuma an fahimci. wanda a kowa, an fahimci false.
Before PHP 4.2.0, the function returned null instead of false when it failed.
If the third parameter strict If specified as true, it only returns the key name of the corresponding element when the data type and value are both consistent.
Syntax
array_search(value,array,strict)
Parameter | Description |
---|---|
value | Required. Specifies the key value to be searched. |
array | Required. Specifies the array to be searched. |
strict |
Optional. If this parameter is set to TRUE, the function searches for elements in the array that have the same data type and value.
If set to true, it checks the type of the given value in the array, the number 5 and the string 5 are different (see example 2). |
Technical Details
Return Value: |
If the specified key value is found in the array, it returns the corresponding key name, otherwise it returns FALSE. If the specified key value is found more than once in the array, it returns the key name that matches the first found key value. |
PHP Version: | 4.0.5+ |
Update Log: |
If invalid parameters are passed to the function, the function returns NULL (this applies to all PHP functions starting from PHP 5.3.0). Starting from PHP 4.2.0, if the search fails, the function returns FALSE instead of NULL. |
More Examples
Example 1
Search for the key value 5 in the array and return its key name (note ""):
<?php $a=array("a"=>"5","b"=>5,"c"=>"5"); echo array_search(5,$a,true); ?>