array_search()
PHP array_search() function is used to search for a given value in an array, and returns the corresponding key name if successful, otherwise it returns FALSE.
Syntax:
mixed array_search( mixed needle, array array [, bool strict] ) parameter description:
参数 | 说明 |
---|---|
needle | 需要在数组中搜索的值,如果是字符串,则区分大小写 |
array | 需要检索的数组 |
strict | 可选,如果设置为 TRUE ,则还会对 needle 与 array 中的值类型进行检查 |
Since the starting index number of the index array may be 0, the function may also return a non-Boolean value equivalent to FALSE, such as 0 or "", so you need to use the === operator to perform Strict verification.
Example:
<?php $arr_a = array(0 => "a", 1 => "b", 2 => "c"); $key = array_search("a", $arr_a); if( $key !== FALSE ){ echo "键名为:$key"; } else { echo '无匹配结果'; } ?>
The example output results are as follows:
Key name: 0 If needle appears more than once in array, return the first matching key. To return the keys of all matching values, use the array_keys() function.
The above PHP search for a given simple instance in an array array_search function is all the content shared by the editor. I hope it can give you a reference, and I hope you will support Bangkejia more.