array_search() - 在数组中搜索给定的值,如果成功则返回首个相应的键名 - php 数组函数
array_search()
(PHP 4 >= 4.0.5, PHP 5, PHP 7)
在数组中搜索给定的值,如果成功则返回首个相应的键名
说明
array_search(mixed $needle,array $haystack[,bool $strict= false]): mixed大海捞针,在大海($haystack)中搜索针($needle参数)。
参数
$needle搜索的值。
Note:如果$needle是字符串,则比较以区分大小写的方式进行。$haystack
这个数组。
$strict如果可选的第三个参数$strict为TRUE
,则array_search()将在$haystack中检查完全相同的元素。这意味着同样严格比较$haystack里$needle的类型,并且对象需是同一个实例。
返回值
如果找到了$needle则返回它的键,否则返回FALSE
。
如果$needle在$haystack中出现不止一次,则返回第一个匹配的键。要返回所有匹配值的键,应该用array_keys()加上可选参数$search_value来代替。
Warning此函数可能返回布尔值FALSE
,但也可能返回等同于FALSE
的非布尔值。请阅读布尔类型章节以获取更多信息。应使用===运算符来测试此函数的返回值。
更新日志
版本 | 说明 |
---|---|
5.3.0 | As with all internal PHP functions as of 5.3.0,array_search()returnsNULL if invalid parameters are passed to it. |
范例
Example #1array_search()例子
参见
array_keys()
返回数组中部分的或所有的键名array_values()
返回数组中所有的值array_key_exists()
检查数组里是否有指定的键名或索引in_array()
检查数组中是否存在某个值
in (PHP 5 >= 5.5.0) you don't have to write your own function to search through a multi dimensional array ex : $userdb=Array ( (0) => Array ( (uid) => '100', (name) => 'Sandra Shush', (url) => 'urlof100' ), (1) => Array ( (uid) => '5465', (name) => 'Stefanie Mcmohn', (pic_square) => 'urlof100' ), (2) => Array ( (uid) => '40489', (name) => 'Michael', (pic_square) => 'urlof40489' ) ); simply u can use this $key = array_search(40489, array_column($userdb, 'uid'));
About searcing in multi-dimentional arrays; two notes on "xfoxawy at gmail dot com"; It perfectly searches through multi-dimentional arrays combined with array_column() (min php 5.5.0) but it may not return the values you'd expect. Since array_column() will produce a resulting array; it won't preserve your multi-dimentional array's keys. So if you check against your keys, it will fail. For example; Here, you could expect that the $found_key would be "5" but it's NOT. It will be 1. Since it's the second element of the produced array by the array_column() function. Secondly, if your array is big, I would recommend you to first assign a new variable so that it wouldn't call array_column() for each element it searches. For a better performance, you could do;
the recursive function by tony have a small bug. it failes when a key is 0 here is the corrected version of this helpful function:
If you are using the result of array_search in a condition statement, make sure you use the === operator instead of == to test whether or not it found a match. Otherwise, searching through an array with numeric indicies will result in index 0 always getting evaluated as false/null. This nuance cost me a lot of time and sanity, so I hope this helps someone. In case you don't know what I'm talking about, here's an example:
$array = ['a', 'b', 'c']; $key = array_search('a', $array); //$key = 0 if ($key) { //even a element is found in array, but if (0) means false //... } //the correct way if (false !== $key) { //.... } It's what the document stated "may also return a non-Boolean value which evaluates to FALSE."
About searcing in multi-dimentional arrays; note on "xfoxawy at gmail dot com" and turabgarip at gmail dot com; $xx = array_column($array, 'NAME', 'ID'); will produce an array like : $xx = [ [ID_val] => NAME_val [ID_val] => NAME_val ] so: $yy = array_search('tesxt', array_column($array, 'NAME', 'ID')); will output expected ID;
/* Be careful!!! when i in php7.1.5 below program return int 0 is it a bug? */ array_search(0,['a','b','c'])
Simple way to get variable name by using array_search function:
for searching case insensitive better this:
hey i have a easy multidimensional array search function
I was going to complain bitterly about array_search() using zero-based indexes, but then I realized I should be using in_array() instead. // if ( isset( $_GET['table'] ) and array_search( $_GET['table'], $valid_tables) ) { // BAD: fails on first[0] element // if ( isset( $_GET['table'] ) and ( FALSE !== array_search( $_GET['table'], $valid_tables) ) ) { OK: but wasteful and convoluted if ( isset( $_GET['table'] ) and in_array( $_GET['table'], $valid_tables) ) { // BETTER The essence is this: if you really want to know the location of an element in an array, then use array_search, else if you only want to know whether that element exists, then use in_array()
Be careful when search for indexes from array_keys() if you have a mixed associative array it will return both strings and integers resulting in comparison errors This happens because PHP, when comparing strings and integers, casts strings TO integers and this results in most of the cases in string becoming 0, so that's why when array_search() compares the first index (0) with the key "car" it gets true because apparently ("car" == 0) IS TRUE. Setting array_search() to strict mode won't solve the problem because then array_search("0", array_keys($arr)) would return false even if an element with index 0 exists. So my solution just converts all indexes from array_keys() to strings and then compares them correctly:
Noted some interesting behaviour when using array_search to find the correct index in an array containing an index with a value of 0. The following works as expected.
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)