Table of Content
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?
// 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 = array("name" => null, "age" => 30);
var_dump(array_key_exists("name", $array)); // true
var_dump(isset($array["name"])); // false
- Use
array_key_existswhen you only need to confirm the key. - Use
issetwhen you also need to confirm the value is not null.
How do you check multiple keys with array_key_exists in PHP?
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?
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
The array_combine function in PHP creates a new array with one array for keys and another for values. It needs…
A complex web application needs proper organization and maintenance to keep your code structured. This is where PHP Object-Oriented Programming…
The PHP named arguments are the names of the arguments through which the values are passed, allowing you to add…
When working with big arrays in PHP, things can get messy. That is where php array_chunk() steps in by providing…
PHP array_find was released in PHP 8.4 to locate a value inside an array and returns the first match. Understand…
Understanding PHP syntax is like the basics of any language—PHP syntax defines the rules for writing code that a server…
The array_shift function in PHP takes the first element from an array and returns it. It also moves all other…
The array_intersect_ukey is used to compare arrays by keys with a custom function in PHP. It returns matches by keys…
Think about the PHP variable as little storage boxes holding your data, whether it is a number, a word, or…
PHP offers four main functions to include files: require, require_once, include, and include_once. Each one gives you a similar purpose…