array_key_exists
Checks whether a key is present in an array, even when its value is null.
array_key_exists(string|int|float|bool|resource|null $key, array $array): bool- Added in PHP 4.0.7
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string|int|float|bool|resource|null | The key to look for. Non-string, non-int keys are coerced the same way array key access coerces them. |
$array | array | The array whose keys are checked. |
Return value
booltrue if the key exists in the array, false otherwise.
Errors
Passing an object or other non-array as $array throws a TypeError.
Changelog
- PHP 8.5.0Passing null as $key is deprecated; pass an empty string instead.
- PHP 8.0.0$key now formally accepts bool, float, int, null, resource and string.
- PHP 8.0.0Passing an object as $array is no longer supported; use property_exists() instead.
- PHP 7.4.0Passing an object as $array was deprecated.
array_key_exists() asks only one thing: is this key in the array? It says nothing about the value, which is exactly what separates it from isset().
The isset difference
isset($a['k']) returns false when the key exists but holds null. array_key_exists('k', $a) returns true. Whenever null is a meaningful value in your data — a nullable database column, an optional setting that was explicitly cleared — isset() gives you the wrong answer and array_key_exists() gives you the right one. When null cannot occur, prefer isset(): it is a language construct with no function-call overhead and it is noticeably faster in hot loops.
One dimension only
It looks at the top level of the array. For nested data you have to check each level, or reach for null-safe access such as $a['x']['y'] ?? null.
Key coercion in PHP 8
The key is coerced the way array subscripting coerces: true becomes 1, 1.9 becomes 1, and the numeric string "5" becomes the integer 5. In PHP 8.5, passing null is deprecated — pass "" instead.
Example
When to use it
- Distinguish a form field that was submitted empty from one that was never submitted at all.
- Check for a nullable database column in a fetched row without treating null as missing.
- Verify a config key is defined before falling back to a default that null would wrongly trigger.
Frequently asked questions
What is the difference between isset and array_key_exists?
array_key_exists returns true for a key whose value is null, while isset returns false. Use array_key_exists when null is a valid value, and isset when it is not — isset is faster.
Does array_key_exists work on multidimensional arrays?
It only checks the first dimension. To reach a nested key, check each level in turn or use the null coalescing operator when null and missing mean the same thing to you.
Can I use array_key_exists on an object?
No. Passing an object was deprecated in PHP 7.4 and removed in PHP 8.0; it now throws a TypeError. Use property_exists() for objects, or cast the object to an array first.
More examples
Submitted empty versus never submitted
All three cases are distinguishable: name was sent (empty string), newsletter was sent as null, and phone never arrived. isset() alone would collapse the last two into one.
<?php
$post = ['name' => '', 'newsletter' => null];
foreach (['name', 'newsletter', 'phone'] as $field) {
$state = array_key_exists($field, $post)
? ($post[$field] === null ? 'sent as null' : 'sent')
: 'not sent';
echo $field, ': ', $state, "\n";
}Checking a nested key
array_key_exists only inspects the level you hand it, so nested checks are chained. isset() and ?? both report the null password as missing, which is why the explicit check is needed here.
<?php
$config = ['db' => ['host' => 'localhost', 'pass' => null]];
// One dimension at a time
$has = array_key_exists('db', $config) && array_key_exists('pass', $config['db']);
var_dump($has);
var_dump(isset($config['db']['pass']));
var_dump($config['db']['pass'] ?? 'default');
Discussion