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.
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.
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)) ...
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;
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 constructisset()
can tell you that (and that the value is notnull
).array_key_exists()
cannot return information about the value of a key/index.The main difference when working on arrays is that
array_key_exists
returnstrue
when the value isnull
, whileisset
will returnfalse
when the array value is set tonull
.See isset on the PHP documentation site.
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
and
array_key_exists
So how do we check if a key exists which value may be null in a variable
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
@
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:which is likely to be faster if the tests are mainly on non-null values. Otherwise for an array with mostly null values
will do the work.
Function
isset()
is faster, check http://www.php.net/manual/en/function.array-key-exists.php#82867array_key_exists
will definitely tell you if a key exists in an array, whereasisset
will only returntrue
if the key/variable exists and is notnull
.There is another important difference.
isset
doesn't complain when$a
does not exist, whilearray_key_exists
does.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?]PS: you can change/correct/complement this text, it is a Wiki.