PHP array_key_exists() function


The array_key_exists() function in PHP is used to check if a specific key or index exists in an array. It returns true if the key is found in the array, and false otherwise. This function works for both associative arrays (with string keys) and indexed arrays (with integer keys).

Syntax:

array_key_exists(mixed $key, array $array): bool

Parameters:

  • $key: The key or index you want to check for in the array. This can be a string or an integer.
  • $array: The array where the key will be searched.

Return Value:

  • Returns true if the key exists in the array.
  • Returns false if the key does not exist.

Important Notes:

  • The function only checks for the existence of a key, not the value. If the key exists but has a null value, the function will still return true.
  • It does not search for values; it only looks at the keys.

Example 1: Basic Usage

<?php $array = [ "name" => "Alice", "age" => 25, "email" => "alice@example.com" ]; // Check if the key "name" exists if (array_key_exists("name", $array)) { echo "Key 'name' exists in the array."; } else { echo "Key 'name' does not exist."; } ?>

Output:

Key 'name' exists in the array.

In this example, array_key_exists() checks if the key "name" exists in the $array. Since it does, the function returns true.

Example 2: Checking for Integer Keys

array_key_exists() works with both associative arrays (string keys) and indexed arrays (integer keys).

<?php $array = [10, 20, 30]; // Check if index 2 exists if (array_key_exists(2, $array)) { echo "Key 2 exists in the array."; } else { echo "Key 2 does not exist."; } ?>

Output:

Key 2 exists in the array.

Here, array_key_exists(2, $array) returns true because the index 2 exists in the array.

Example 3: Checking for a Key with a null Value

Even if a key exists but its value is null, array_key_exists() will still return true.

<?php $array = [ "name" => "Bob", "age" => null ]; // Check if the key "age" exists if (array_key_exists("age", $array)) { echo "Key 'age' exists in the array."; } else { echo "Key 'age' does not exist."; } ?>

Output:

Key 'age' exists in the array.

Even though the value of "age" is null, the key exists, so the function returns true.

Example 4: Comparing with isset()

There is a difference between array_key_exists() and isset(). The isset() function will return false if a key exists but its value is null, while array_key_exists() will return true.

<?php $array = [ "name" => "Charlie", "age" => null ]; // Check using isset() var_dump(isset($array["age"])); // Outputs: bool(false) // Check using array_key_exists() var_dump(array_key_exists("age", $array)); // Outputs: bool(true) ?>

In this case, isset($array["age"]) returns false because the value of "age" is null, but array_key_exists("age", $array) returns true because the key "age" does exist.

Practical Usage:

  • array_key_exists() is useful for:
    • Checking if a specific key exists in an associative array before accessing or modifying it.
    • Safely verifying the presence of a key without causing errors, especially when dealing with user input or dynamic data structures.
    • Avoiding undefined index errors when accessing arrays.

Summary:

  • array_key_exists($key, $array) checks if a key exists in an array.
  • It works with both associative arrays and indexed arrays.
  • The function returns true if the key exists and false otherwise, regardless of the value (even if it's null).
  • It's different from isset(), which returns false if the value is null.