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 array_combine: How to Merge Arrays as Key and Value

The array_combine function in PHP creates a new array with one array for keys and another for values. It needs…

PHP OOP Programming: A Complete Tutorial

A complex web application needs proper organization and maintenance to keep your code structured. This is where PHP Object-Oriented Programming…

PHP Named Arguments vs. Positional Arguments

The PHP named arguments are the names of the arguments through which the values are passed, allowing you to add…

PHP array_chunk(): Split Arrays into Manageable Parts

When working with big arrays in PHP, things can get messy. That is where php array_chunk() steps in by providing…

PHP array_find: How to Locate Array Values with Examples

PHP array_find was released in PHP 8.4 to locate a value inside an array and returns the first match. Understand…

PHP Syntax: Standard Tags, Echo, and Short Tags

Understanding PHP syntax is like the basics of any language—PHP syntax defines the rules for writing code that a server…

PHP array_shift: Remove the First Array Element with Examples

The array_shift function in PHP takes the first element from an array and returns it. It also moves all other…

PHP array_intersect_ukey Function: How it Works with Examples

The array_intersect_ukey is used to compare arrays by keys with a custom function in PHP. It returns matches by keys…

PHP Variable Basics: Everything You Need to Know

Think about the PHP variable as little storage boxes holding your data, whether it is a number, a word, or…

PHP File Inclusion: require, include, require_once, include_once

PHP offers four main functions to include files: require, require_once, include, and include_once. Each one gives you a similar purpose…

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.