PHP Reference ยท Array Functions
PHP array_key_exists() function
Reference for PHP array_key_exists(): check whether a key is present in an array, how it differs from isset() for null values, and key type rules.
The array_key_exists() function tells you whether a key is present in an array. It returns true even when the value stored under that key is null, which is the main reason to choose it over isset().
Use it whenever null is a legitimate value, such as a nullable database column or an optional setting that was explicitly set to null.
$user = ["name" => "Ada", "email" => null]; var_dump(array_key_exists("email", $user)); // true var_dump(isset($user["email"])); // false
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
array_key_exists(string|int|float|bool|resource|null $key, array $array): bool
| Name | Description |
|---|---|
$key |
The key to look for. Converted by the normal array key rules. |
$array |
The array to search. |
Return value: true if the key exists, otherwise false.
Errors / Exceptions: a TypeError if the second argument is not an array, or if the key is an array or object.
Usage notes
nullvalues count as present.isset($a["k"])isfalsefornull;array_key_exists("k", $a)istrue.isset()and??are usually enough and a little faster whennulland "missing" mean the same thing.- Key conversion applies:
"5"is integer 5 and booleans become 0 or 1. Passingnullas the key is deprecated in PHP 8.5; use""instead. - Objects are no longer accepted. Using
array_key_exists()on an object was removed in PHP 8.0; useproperty_exists()orisset($obj->prop). key_exists()is an alias of the same function.- Not a value search: to check a value use
in_array(); to find the key of a value usearray_search(). - Performance: a key lookup is a hash lookup, constant time. It is far faster than
in_array()on large arrays, so for repeated membership tests flip values into keys witharray_flip().