本文翻译自:What's the difference between isset() and array_key_exists()? [duplicate]
This question already has an answer here: 这个问题在这里已有答案:
How do the following two function calls compare: 以下两个函数调用如何比较:
isset($a['key'])
array_key_exists('key', $a)
#1楼
参考:https://stackoom.com/question/DTJH/isset-和array-key-exists-之间有什么区别-重复
#2楼
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;array_key_exists
:如果数组中存在键,则为true;否则为false。 -
isset
: istrue
if the key/variable exists and is notnull
[ faster than array_key_exists ];isset
:如果键/变量存在且为非null
则为true
[ 比array_key_exists更快 ]; -
@$array['key']
: istrue
if the key/variable exists and is not (null
or '' or 0) ;@$array['key']
:如果键/变量存在且不存在,则为true
(null
或''或0) ; [so much slower?] [那么慢?]
$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。
#3楼
The PHP function array_key_exists()
determines if a particular key, or numerical index, exists for an element of an array. PHP函数array_key_exists()
确定数组元素是否存在特定键或数字索引。 However, if you want to determine if a key exists and is associated with a value , the PHP language construct isset()
can tell you that (and that the value is not null
). 但是,如果要确定某个键是否存在并且是否与值相关联,则PHP语言构造isset()
可以告诉您(并且该值不为null
)。 array_key_exists()
cannot return information about the value of a key/index. array_key_exists()
无法返回有关键/索引值的信息。
#4楼
Answer to an old question as 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 ... 如果未定义var,则不会出现警告或错误,而不是数组......
- but returns false if the value for that key is null 但如果该键的值为null ,则返回false
and array_key_exists
和array_key_exists
- tells if a key exists in an array as the name implies 根据名称暗示是否存在数组中的键
- but gives 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 那么我们如何检查一个键是否存在变量中可能为null的值
- 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 1 st dim (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). 没有得到警告,当它的值为null时没有遗漏现有的密钥(PHP开发人员的想法也是一个有趣的问题,但在SO上肯定不相关)。 And of course we don't want to use @
当然我们不想用@
isset($var[$key]); // silent but misses null values
array_key_exists($key, $var); // works but warning if $var not defined/array
It seems is_array
should be involved in the equation, but it gives a warning if $var
is 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. 会做的工作。
#5楼
函数isset()
更快,请查看http://www.php.net/manual/en/function.array-key-exists.php#82867
#6楼
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. 普遍的共识似乎是尽可能使用isset,因为它是一种语言结构,因此更快。 However, the differences should be outlined above. 但是,差异应该在上面概述。