Skip to content
Tutorials Center
My learning

Topics

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.

PHP
$user = ["name" => "Ada", "email" => null];
var_dump(array_key_exists("email", $user)); // true
var_dump(isset($user["email"]));            // false

Try it

PHP array_key_exists() versus isset()

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.

PHP Key types

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

PHP
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

  • null values count as present. isset($a["k"]) is false for null; array_key_exists("k", $a) is true.
  • isset() and ?? are usually enough and a little faster when null and "missing" mean the same thing.
  • Key conversion applies: "5" is integer 5 and booleans become 0 or 1. Passing null as 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; use property_exists() or isset($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 use array_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 with array_flip().