PHP array_key_exists: How it Works with Examples

php array_key_exists function

The PHP array_key_exists function checks a given key in an array and tells if that key exists or not.

Understand the array_key_exists in PHP

The array_key_exists function in PHP checks a key in an array and returns true if the key exists.

Here is the syntax:

array_key_exists($key, $array)

You write it to check the key. The first input is the key and the second input is the array. It returns true if the key is present and false if the key is not present.

The function reads the array, compares each key with the key you pass, and then gives true or false. This process happens without changing the data in the array.

You can pass an associative array with named keys to this function and it checks if a specific name exists as a key.

The Difference Between array_key_exists and isset

The isset function checks if a key exists and also if the value is not null. The array_key_exists function only checks if the key exists and ignores the value.

The array_key_exists function gives true even if the value of that key is null. The isset function gives false if the key value is null even if the key exists. This difference matters when you need to detect keys with null values.

A developer can use array_key_exists to detect all keys including keys with null values. The same developer can use isset to detect only keys with values not equal to null.

Examples of the array_key_exists Function in PHP

Check a key in a simple array:

$array = ['name' => 'John', 'age' => 30];
if (array_key_exists('name', $array)) {
    echo "The key exists.";
}

This example checks if the key ‘name’ exists in an array with name and age. It prints a message because the key is present.

Detect a key with null value:

$array = ['name' => null, 'age' => 30];
if (array_key_exists('name', $array)) {
    echo "The key exists even if the value is null.";
}

This example shows that array_key_exists returns true even if the key has a null value. It prints the message to show this case.

Compare array_key_exists and isset:

$array = ['name' => null, 'age' => 30];
if (isset($array['name'])) {
    echo "isset true";
} else {
    echo "isset false";
}

This example shows that isset returns false when the key exists but holds null. It explains why developers choose array_key_exists to detect such keys.

Check a dynamic key from user input:

$array = ['username' => 'admin', 'role' => 'editor'];
$key = 'role';
if (array_key_exists($key, $array)) {
    echo "The key from user input exists.";
}

This example shows a dynamic key passed from a variable to the function. It prints the message because the key exists in the array.

Wrapping Up

You learned what array_key_exists does and how to use it with arrays, and how it differs from isset.

Here is a quick recap:

  • The array_key_exists checks only if a key exists, while isset checks both the key and the value.
  • You also saw examples of simple and advanced use.

FAQs

What is PHP array_key_exists used for?

PHP array_key_exists is used to check if a specific key exists in an array.

// Example
$array = array("name" => "John", "age" => 30);

if(array_key_exists("name", $array)) {
    echo "Key exists!";
} else {
    echo "Key does not exist!";
}
Output: Key exists!
  • Checks for key, not value
  • Returns true if key exists, false otherwise

What is the difference between PHP array_key_exists and isset?

array_key_exists checks if a key exists in the array, even if its value is NULL. isset checks if a key exists and its value is not NULL.

$array = array("name" => null, "age" => 30);

var_dump(array_key_exists("name", $array)); // true
var_dump(isset($array["name"]));            // false
  1. Use array_key_exists when you only need to confirm the key.
  2. Use isset when you also need to confirm the value is not null.

How do you check multiple keys with array_key_exists in PHP?

You can loop through an array of keys and use array_key_exists for each one.

$array = array("id" => 101, "name" => "Alice", "age" => 25);
$keys = array("id", "name", "email");

foreach($keys as $key) {
    if(array_key_exists($key, $array)) {
        echo "Key $key exists<br>";
    } else {
        echo "Key $key not found<br>";
    }
}
Output:
  • Key id exists
  • Key name exists
  • Key email not found

Can PHP array_key_exists be used with objects?

Yes, but it only works on arrays. For objects, use property_exists.

class User {
    public $name;
    public $age;
}

$user = new User();
$user->name = "Mark";
$user->age = 40;

var_dump(property_exists($user, "name")); // true
var_dump(property_exists($user, "email")); // false
Tip: Use array_key_exists for arrays and property_exists for objects.

Similar Reads

PHP fclose(): Close Access of File Manipulation

Actually, PHP has a built-in function that doesn’t get the spotlight—fclose(). It ends the file manipulation code. Let’s break down…

PHP trim(): How to remove Whitespace from String

Early PHP scripts often failed because of extra spaces or line breaks in strings. These issues came from user input…

Understanding the PHP Exclusive OR (XOR) Operator

In PHP, there is this interesting operator known as "exclusive OR," or just XOR. It is somewhat of an underdog…

PHP Assignment Operators: A Complete Guide

The assignment operators are the sets of gears that keep your code well-oiled and running when deep in PHP, allowing…

PHP Magic Constants: Simplify Your Code

There are some tools known as "PHP magic constants" that will make your code a little more clever and versatile.…

PHP htmlspecialchars Function: Prevent XSS in HTML Output

A PHP script can break the page or allow code injection if it outputs user input directly into HTML. The…

PHP array_all Function: How it Works with Examples

PHP 8.4 released a new built-in function, array_all checks to check if all array values meet a condition. It gives…

PHP range Function: Create Arrays with Sequence Values

The PHP range function helps you make sequences. Without it, you would write loops every time you need numbers or…

PHP DOMDocument: XML DOM Parser Guide

In PHP, if you're working with XML files, you’ll probably work with DOMDocument. It is a powerful class, enabling you…

How to Select Data Using PHP and MySQL?

In this article, You will understand everything you need to know about how to use PHP to select data from…

Previous Article

Git add Command with Step by Step with Example

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.