PHP array_key_exists() Function

Quick summary: The PHP array_key_exists() function checks if a key exists in an array.

PHP array_key_exists() Syntax

array_key_exists(string|int $key, array $array): bool
PHP

PHP array_key_exists() Basic examples

var_dump(array_key_exists('a', ['a'=>1]));
PHP
Output:
bool(true)

Returns true if the key exists.

var_dump(array_key_exists('b', ['a'=>null]));
PHP
Output:
bool(false)

Returns false when the key does not exist.

PHP array_key_exists() Real-world usage

$data = ['id'=>null];
if (array_key_exists('id', $data)) {
    echo 'Key exists';
}
PHP
Output:
Key exists

Detects keys even when values are null.

PHP array_key_exists() Edge cases

var_dump(isset(['a'=>null]['a']));
PHP
Output:
bool(false)

isset returns false for null values.

PHP array_key_exists() Common mistakes

Confusing isset with array_key_exists

isset returns false for null values.

Incorrect
isset(['a'=>null]['a']);
Correct
array_key_exists('a', ['a'=>null]);

Use array_key_exists to detect null keys.

PHP array_key_exists() Frequently Asked Questions

What does array_key_exists() do?

Checks if a key exists in an array.

Return type?

Boolean.

Difference from isset()?

isset() returns false for null values.

Works with null values?

Yes.

Supports associative arrays?

Yes.

Use case?

Checking key existence safely.

Does it modify array?

No.

Performance?

Fast.

Supports objects?

No.

Handles numeric keys?

Yes.

Case-sensitive?

Yes.

Alternative?

isset().

PHP array_key_exists() Related PHP Functions