php isset() 和 array_key_exists() 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3210935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between isset() and array_key_exists()?
提问by Zacky112
How do the following two function calls compare:
以下两个函数调用如何比较:
isset($a['key'])
array_key_exists('key', $a)
回答by deceze
array_key_existswill definitely tell you if a key exists in an array, whereas issetwill only return trueif the key/variable exists and is not null.
array_key_exists肯定会告诉你一个键是否存在于数组中,而isset只有true当键/变量存在而不是时null才会返回。
$a = array('key1' => 'フーバー', 'key2' => null);
isset($a['key1']); // true
array_key_exists('key1', $a); // true
isset($a['key2']); // false
array_key_exists('key2', $a); // true
There is another important difference: issetdoesn't complain when $adoes not exist, while array_key_existsdoes.
还有一个重要的区别:isset不$a存在时不抱怨,而array_key_exists确实存在。
回答by Patrick Fisher
Between array_key_existsand isset, though both are very fast [O(1)], issetis significantly faster. If this check is happening many thousands of times, you'd want to use isset.
array_key_exists和之间isset,虽然两者都非常快[O(1)],但isset明显更快。如果此检查发生数千次,您会想要使用isset.
It should be noted that they are not identical, though -- when the array key exists but the value is null, issetwill return falseand array_key_existswill return true. If the value may be null, you need to use array_key_exists.
应当指出的是,他们是不相同的,虽然-当数组项存在,但价值null,isset将返回false并array_key_exists返回true。如果值可能是null,则需要使用array_key_exists.
回答by e2-e4
Answer to an old questionas no answer here seem to address the 'warning' problem (explanation follows)
回答一个老问题,因为这里没有答案似乎解决了“警告”问题(解释如下)
Basically, in this case of checking if a key exists in an array, isset
基本上,在检查数组中是否存在键的情况下, isset
- tells if the expression (array) is defined, and the key is set
- no warning or error if the var is not defined, not an array ...
- butreturns falseif the value for that key is null
- 告诉表达式(数组)是否已定义,键是否已设置
- 如果 var 未定义,则没有警告或错误,而不是数组...
- 但如果该键的值为空则返回false
and array_key_exists
和 array_key_exists
- tells if a key exists in an array as the name implies
- butgives a warning if the array parameter is not an array
- 顾名思义,告诉数组中是否存在键
- 但如果数组参数不是数组,则会发出警告
So how do we check if a key exists which value may be null in a variable
那么我们如何检查变量中是否存在某个值可能为空的键
- that may or may not be an array
- (or similarly is a multidimensional array for which the key check happens at dim 2 and dim 1 value may not be an array for the 1stdim (etc...))
- 可能是也可能不是数组
- (或者类似地是多维数组的量,密钥检查发生在昏暗2和变暗1值可能不为1个的阵列第一暗淡(等...))
without getting a warning, without missing the existing key when its value is null(what were the PHP devs thinking would also be an interesting question, but certainly not relevant on SO). And of course we don't want to use @
没有收到警告,也不会在其值为null时遗漏现有的键(PHP 开发人员的想法也是一个有趣的问题,但肯定与 SO 无关)。当然我们不想使用@
isset($var[$key]); // silent but misses null values
array_key_exists($key, $var); // works but warning if $var not defined/array
It seems is_arrayshould be involved in the equation, but it gives a warning if $varis not defined, so that could be a solution:
似乎is_array应该包含在等式中,但如果$var未定义,则会发出警告,因此这可能是一个解决方案:
if (isset($var[$key]) ||
isset($var) && is_array($var) && array_key_exists($key, $var)) ...
which is likely to be faster if the tests are mainly on non-null values. Otherwise for an array with mostly null values
如果测试主要针对非空值,这可能会更快。否则对于大部分为空值的数组
if (isset($var) && is_array($var) && array_key_exists($key, $var)) ...
will do the work.
会做的工作。
回答by Matijs
回答by Anthony Rutledge
The PHP function array_key_exists()determines if a particular key, or numerical index, exists for an element of an array. However, if you want to determine if a keyexists and is associated with a value, the PHP language construct isset()can tell you that (and that the value is not null). array_key_exists()cannot return information about the value of a key/index.
PHP 函数array_key_exists()确定数组元素是否存在特定键或数字索引。但是,如果您想确定一个键是否存在并与一个值相关联,PHP 语言结构isset()可以告诉您(并且该值不是null)。array_key_exists()无法返回有关键/索引值的信息。
回答by Anax
Function isset()is faster, check http://www.php.net/manual/en/function.array-key-exists.php#82867
功能isset()更快,查看http://www.php.net/manual/en/function.array-key-exists.php#82867
回答by Peter Krauss
Complementing (as an algebraic curiosity) the @deceze answer with the @operator, and indicating cases where is "better" to use @... Not really better if you need (no log and) micro-performance optimization:
用@运算符补充(作为代数好奇心)@deceze 的答案,并指出“更好”使用的情况@……如果您需要(无日志和)微性能优化,那就不是更好了:
array_key_exists: is true if a key exists in an array;isset: istrueif the key/variable exists and is notnull[faster than array_key_exists];@$array['key']: istrueif the key/variable exists and is not (nullor '' or 0); [so much slower?]
array_key_exists: 如果键存在于数组中,则为真;isset: 是true如果键/变量存在并且不是null[比 array_key_exists 快];@$array['key']: 是true如果键/变量存在并且不null存在(或 '' 或 0); 【这么慢?】
$a = array('k1' => 'HELLO', 'k2' => null, 'k3' => '', 'k4' => 0);
print isset($a['k1'])? "OK $a[k1].": 'NO VALUE.'; // OK
print array_key_exists('k1', $a)? "OK $a[k1].": 'NO VALUE.'; // OK
print @$a['k1']? "OK $a[k1].": 'NO VALUE.'; // OK
// outputs OK HELLO. OK HELLO. OK HELLO.
print isset($a['k2'])? "OK $a[k2].": 'NO VALUE.'; // NO
print array_key_exists('k2', $a)? "OK $a[k2].": 'NO VALUE.'; // OK
print @$a['k2']? "OK $a[k2].": 'NO VALUE.'; // NO
// outputs NO VALUE. OK . NO VALUE.
print isset($a['k3'])? "OK $a[k3].": 'NO VALUE.'; // OK
print array_key_exists('k3', $a)? "OK $a[k3].": 'NO VALUE.'; // OK
print @$a['k3']? "OK $a[k3].": 'NO VALUE.'; // NO
// outputs OK . OK . NO VALUE.
print isset($a['k4'])? "OK $a[k4].": 'NO VALUE.'; // OK
print array_key_exists('k4', $a)? "OK $a[k4].": 'NO VALUE.'; // OK
print @$a['k4']? "OK $a[k4].": 'NO VALUE.'; // NO
// outputs OK 0. OK 0. NO VALUE
PS: you can change/correct/complement this text, it is a Wiki.
PS:您可以更改/更正/补充此文本,它是一个Wiki。
回答by TNi
The two are not exactly the same. I couldn't remember the exact differences, but they are outlined very well in What's quicker and better to determine if an array key exists in PHP?.
两者并不完全相同。我不记得确切的区别,但它们在什么更快更好地确定 PHP 中是否存在数组键中得到了很好的概述?.
The common consensus seems to be to use isset whenever possible, because it is a language construct and therefore faster. However, the differences should be outlined above.
普遍的共识似乎是尽可能使用 isset,因为它是一种语言结构,因此速度更快。但是,上面应概述这些差异。

