How to check a key exists in an array in PHP ?
We have given an array arr and a Key key, the task is to check if a key exists in an array or not in PHP.
Examples:
Input : arr = ["Geek1", "Geek2", "1", "2","3"]
key = "2"
Output : Found the Key
Input : arr = ["Geek1", "Geek2", "1", "2","3"]
key = 9
Output : Key not Found
The problem can be solved using PHP inbuilt function for checking key exists in a given array. The in-built function used for the given problem are:
Table of Content
Method 1: Using array_key_exists() Method:
The array_key_exists() function checks whether a specific key or index is present inside an array or not.
Syntax:
boolean array_key_exists( $index, $array )
Example:
<?php
// PHP program to check if a key
// exists in an array or not
$array = array(
'names' => array("Geek1", "Geek2", "Geek3"),
'rank' => array('1', '2', '3')
);
// Use of array_key_exists() function
if(array_key_exists("rank", $array)) {
echo "Found the Key";
}
else{
echo "Key not Found";
}
?>
Output
Found the Key
Method 2: Using isset() Method:
The isset() function checks whether a specific key or index is present inside an array or not.
Syntax:
bool isset( mixed $var, mixed $... )
<?php
// PHP program to check if a key
// exists in an array or not
$array = array(
'names' => array("Geek1", "Geek2", "Geek3"),
'rank' => array('1', '2', '3')
);
// Use of array_key_exists() function
if(isset($array["rank"])){
echo "Found the Key";
}
else{
echo "Key not Found";
}
?>
Output
Found the Key
Method 3: Using the array_search function:
The array_search function is used to search for a given value in an array and return the corresponding key if the value is found. If the value is not found, it returns false.
Syntax:
bool = array_search($search_value, $array);
<?php
$array = array('name' => 'geek', 'age' => '22');
if (array_search('geek', $array) !== false) {
echo 'Found the key';
} else {
echo 'Key not found';
}
?>
Output
Found the key
Method 4: Using array_keys
Using array_keys to check if a key exists involves getting all the keys of the array with array_keys and then using in_array to determine if the specific key is present
Example
<?php
$array = ['name' => 'John', 'age' => 30, 'city' => 'New York'];
$key = 'age';
if (in_array($key, array_keys($array))) {
echo "Key '$key' exists in the array.";
} else {
echo "Key '$key' does not exist in the array.";
}
?>
Output
Key 'age' exists in the array.
Using array_key_exists() with array_flip()
This approach involves flipping the array's keys and values using array_flip(), then using array_key_exists() to check if the key exists in the flipped array.
Example:
<?php
function keyExistsUsingArrayFlip($array, $key) {
$flippedArray = array_flip($array);
return array_key_exists($key, $flippedArray);
}
// Example usage:
$arr = ["Geek1", "Geek2", "1", "2", "3"];
$key = "2";
if (keyExistsUsingArrayFlip($arr, $key)) {
echo "Found the Key";
} else {
echo "Key not Found";
}
?>
Output
Found the Key
Using in_array Function Directly
The in_array function checks if a specific value exists in an array, returning true if found and false otherwise. It's used for value existence checks, not for keys.
Example
<?php
$arr = ["Geek1", "Geek2", "1", "2", "3"];
$key = "2";
if (in_array($key, $arr)) {
echo "Found the Key";
} else {
echo "Key not Found";
}
?>
Output
Found the Key