What's the difference between isset() and array_key_exists()?
PhpPhp Problem Overview
How do the following two function calls compare:
isset($a['key'])
array_key_exists('key', $a)
Php Solutions
Solution 1 - Php
array_key_exists
will definitely tell you if a key exists in an array, whereas isset
will only return true
if the key/variable exists and is not 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: isset
doesn't complain when $a
does not exist, while array_key_exists
does.
Solution 2 - Php
Between array_key_exists
and isset
, though both are very fast [O(1)]
, isset
is significantly faster. If this check is happening many thousands of times, you'd want to use isset
.
It should be noted that they are not identical, though -- when the array key exists but the value is null
, isset
will return false
and array_key_exists
will return true
. If the value may be null
, you need to use array_key_exists
.
As noted in comments, if your value may be null
, the fast choice is:
isset($foo[$key]) || array_key_exists($key, $foo)
Solution 3 - Php
The main difference when working on arrays is that array_key_exists
returns true
when the value is null
, while isset
will return false
when the array value is set to null
.
See isset on the PHP documentation site.
Solution 4 - Php
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
- 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 ...
- but returns false if the value for that key is null
and 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
- 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 1st dim (etc...))
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 @
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:
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.
Solution 5 - Php
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 key exists 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.
Solution 6 - Php
Function isset()
is faster, check http://www.php.net/manual/en/function.array-key-exists.php#82867
Solution 7 - Php
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:
array_key_exists
: is true if a key exists in an array;isset
: istrue
if the key/variable exists and is notnull
[faster than array_key_exists];@$array['key']
: istrue
if the key/variable exists and is not (null
or '' or 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.
Solution 8 - Php
The two are not exactly the same. I couldn't remember the exact differences, but they are outlined very well in https://stackoverflow.com/questions/700227/whats-quicker-and-better-to-determine-if-an-array-key-exists-in-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.