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 $GLOBALS: Access Global Variables with Examples

When you start working with PHP, you’ll soon need access to variables from multiple places in your code. For this,…

Install PHP on Operating System: Ubuntu, Windows, and macOS

In this tutorial, you will learn how to install PHP on your Operating system such as Ubuntu, Windows, or macOS.…

PHP sizeof: How to Count Elements in Arrays with Examples

The sizeof function in PHP exists to help developers know how many items an array holds. Understand the sizeof Function…

How to Insert Data into MySQL with PHP

pplications. Such As as From registering new users to collecting form submissions and storing product details. Things like adding a…

Understanding the PHP Operator Precedence

The PHP operator precedence refers to when doing a calculation for three or more numbers, they are calculating its values…

MongoDB CRUD with PHP: Create, Read, Update, and Delete

PHP gives us the ability to work with MongoDB, highlighting the importance of CRUD (Create, Read, Update, Delete) operations for…

PHP Logical Operators | Understanding the 6 Logical Operators

The PHP logical operators are operands that can be used to check two or more expressions together. For examples (…

filter_has_var Function: Check if Input Exists in PHP

User input does not always come through as expected. Sometimes values are missing or not set at all. This can…

PHP strpos Function: How it Works with Examples

Sometimes the string you expect is there, but strpos function gives a surprise in PHP. It may show the result…

PHP Predefined Constants Explained Simply

PHP has a number of predefined constants that give you some information about the language and its environment. You can…

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.