Content · Programming · PHP

PHP array_key_exists() Function: Usage and Examples

Learn how to use PHP array_key_exists() to check whether array keys exist, handle null values, validate required fields, and work with associative arrays.

4 views Published Oct 16, 2026 Updated Oct 16, 2026 Admin

When working with PHP arrays, you often need to check whether a particular key exists before accessing its value. This is especially useful when processing form submissions, configuration settings, or data received from an API.

The array_key_exists() function checks whether an array contains a specific key, even when its corresponding value is null.

What Is array_key_exists() in PHP?

The array_key_exists() function checks whether a key or numeric index exists in an array.

Its basic syntax is:

array_key_exists($key, $array): bool

The function accepts two parameters:

  • $key — The key or index you want to find.
  • $array — The array to search.

It returns true when the key exists and false when it doesn't.

The most common keys are strings and integers. PHP also accepts certain other key types through its normal array-key conversion rules.

Example 1: Checking an Associative Array

Let's check whether a user record contains an email address field.

<?php

$user = [
    "name" => "Alex",
    "email" => "[email protected]"
];

var_dump(array_key_exists("email", $user));
var_dump(array_key_exists("phone", $user));

Output:

bool(true)
bool(false)

The email key exists, so the first check returns true. The phone key is missing, so the second returns false.

PHP array keys are case-sensitive. For example, "Email" and "email" are different keys.

Example 2: Working with Numeric Keys

The function also works with indexed arrays, including arrays with non-sequential keys.

<?php

$products = [
    10 => "Keyboard",
    25 => "Mouse"
];

var_dump(array_key_exists(25, $products));
var_dump(array_key_exists(0, $products));

Output:

bool(true)
bool(false)

The key 25 exists, while key 0 does not. The function checks actual array keys, not element positions or values.

array_key_exists() vs isset(): What's the Difference?

The most important distinction is how these functions handle null values.

<?php

$settings = [
    "theme" => "dark",
    "timezone" => null
];

var_dump(array_key_exists("timezone", $settings));
var_dump(isset($settings["timezone"]));

Output:

bool(true)
bool(false)

Both functions check whether an array entry is available, but they answer slightly different questions:

Condition array_key_exists() isset()
Key exists with a value true true
Key exists with null true false
Key does not exist false false

Use array_key_exists() when you need to distinguish a missing key from one explicitly assigned null. Use isset() when you need to know whether an entry exists and is not null.

Practical Example: Updating User Preferences

Imagine your application receives a partial profile update. A missing field means the user doesn't want to change it, while a field containing null means the user wants to clear its current value.

Let's use array_key_exists() to distinguish those situations.

Create a file named update-profile.php:

<?php

declare(strict_types=1);

// Existing profile.

$profile = [
    "name" => "Alex",
    "phone" => "555-1234",
    "city" => "London"
];

// Example incoming update.

$updates = [
    "phone" => null
];

// Update the phone only if the field was supplied.

if (array_key_exists("phone", $updates)) {

    $phone = $updates["phone"];

    if ($phone === null || is_string($phone)) {
        $profile["phone"] = $phone;
    }
}

// Display the updated profile.

echo "<h2>User Profile</h2>";

foreach ($profile as $field => $value) {

    $displayValue = $value === null
        ? "Not provided"
        : (string) $value;

    $message = $field . ": " . $displayValue;

    echo "<p>" . htmlspecialchars(
        $message,
        ENT_QUOTES,
        "UTF-8"
    ) . "</p>";
}

Browser output:

User Profile

name: Alex
phone: Not provided
city: London

The phone number is cleared because the phone key exists in the update, even though its value is null.

If the key were missing entirely, the original phone number would remain unchanged.

This distinction is useful for partial API updates, optional form fields, and configuration changes. Real applications should also validate submitted values and verify that users are authorized to modify each field.

Checking Keys in Nested Arrays

The function examines only the array you pass to it. It does not search nested arrays automatically.

<?php

$user = [
    "profile" => [
        "email" => "[email protected]"
    ]
];

$exists = isset($user["profile"])
    && is_array($user["profile"])
    && array_key_exists(
        "email",
        $user["profile"]
    );

var_dump($exists);

Output:

bool(true)

The code first confirms that profile exists and contains an array, then checks for the nested email key.

Common Mistakes

  • Confusing keys with values: Use in_array() to search for a value instead.
  • Using isset() when null is meaningful: Use array_key_exists() to detect keys containing null.
  • Expecting recursive searches: Check the appropriate nested array explicitly.
  • Passing an object instead of an array: PHP 8+ requires the second argument to be an array. Use property_exists() for object properties.
  • Using null as the key: Passing null as the key is deprecated in PHP 8.5. Use a suitable explicit key instead.

Key Takeaways

  • array_key_exists() checks whether a specified array key exists.
  • It returns true even when the associated value is null.
  • It works with string and numeric array keys.
  • It checks only the supplied array, not nested arrays automatically.
  • It is useful for validating field presence and processing partial updates.

Use array_key_exists() whenever the presence of a key matters independently of its value. It helps make array handling more predictable, especially when missing fields and explicitly empty values have different meanings.

For additional details, see the official PHP array_key_exists() documentation.

Share this resourceSend this page to someone who may find it useful.