What is PHP key_exists() Function?
To check whether a specific key or index exists in an array, use key_exists() function. The function doesn’t care whether the value of the key exists or not.
Syntax:
key_exists(key, array)
Parameters:
The Function has 2 parameters; both are required-
key (Required): This is an array key that we will search within the array. It could be a string or integer or float or bool or resource or null. Check example 1.
array (Required): This is an array into which we will search the index.
Return Values:
The function returns-
- TRUE – If it finds the key in the array.
- FALSE – If it doesn’t find the key in the array.
How to use key_exists() in coding?
As the function returns a Boolean value TRUE/FALSE, you can check this by conditional statements i.e. if statement, ternary operator. Choose one that suits your requirement. Depending on the result the conditional statement returns, your program flow progresses. Have a look at the code-
<?php
i$arr = array("PHP","Python", "Java");
if(key_exists(2, $arr)){
echo "Array key exists.";
}else{
echo "Array key doesn't exists";
}
?>
Examples:
Example 1: Simple key_exists() function examples-
<?php
$arr1 = array("one"=>11,"two"=>22,"three"=>33); echo array_key_exists("two", $arr1)? "Key exists.":"Key doesn't exist."; echo "<br />";
$arr1 = array(11,22,33); echo array_key_exists(1, $arr1)? "Key exists.":"Key doesn't exist."; echo "<br />";
$arr1 = array(11.5=>11,22.3=>22,33.7=>33); echo array_key_exists(22.3, $arr1)? "Key exists.":"Key doesn't exist."; echo "<br />";
$arr1 = array(TRUE=>11,FALSE=>22); echo array_key_exists(TRUE, $arr1)? "Key exists.":"Key doesn't exist."; echo "<br />";
$arr1 = array(11.5=>11,22.3=>22,33.7=>33,NULL=>44); echo array_key_exists(NULL, $arr1)? "Key exists.":"Key doesn't exist."; echo "<br />";
$arr1 = array(11,22,33); echo array_key_exists(3, $arr1)? "Key exists.":"Key doesn't exist."; echo "<br />";
?>
Output:
Key exists.
Key exists.
Key exists.
Key exists.
Key exists.
Key doesn’t exist.
Explanation:
Line 2: the key_exists() uses an array that has string type keys.
Line 3: the key_exists() uses an array that has integer type keys.
Line 4: the key_exists() uses an array that has float type keys.
Line 5: the key_exists() uses an array that has Boolean type keys.
Line 6: the key_exists() uses an array that has NULL type key.
Example 2: Checking an array element existence before accessing it-
<?php
$arr = array(24, 25, 26, 27, 28);
if(key_exists(10, $arr)){
echo "The value is: " . $arr[10];
// Do further coding.
}else{
echo "Array key doesn't exist.";
}
?>
Output:
Array key doesn’t exist.
Explanation:
There are 5 keys in the array – 0, 1, 2, 3, 4. So, the key “10” doesn’t exist in the array. So, it is better to check whether the key exists in the array before doing further.
Example 3: Avoiding “Undefined array key” with key_exists()-
<?php
$arr = array("Name", "Age", "Language");
echo $arr[3];
if(key_exists(2, $arr)){
echo "The value is: " . $arr[2];
}
?>
Output:
Warning: Undefined array key 3 in D:\xampp\htdocs\key_exists function.php on line 3
The value is: Language
Explanation:
Line 3: the key 3 doesn’t exist in the array $arr. So, the “Undefined array key” error occurs. So, you better check its existence at first.
Example 4: key_exists() with indexed and associative array-
<?php
$arr1 = array("PHP", "Python", "Java"); echo key_exists(2, $arr1)? "Key exists.":"Key doesn't exist."; echo "<br />";
$arr2 = array("one"=>"PHP", "two"=>"Python", "three"=>"Java"); echo key_exists("two", $arr2)? "Key exists.":"Key doesn't exist.";
?>
Output:
Key exists.
Key exists.
Explanation:
Line 2: $arr1 is an indexed array and key_exists() function can work on this keys.
Line 3: $arr1 is an associative array and key_exists() function can work on this keys.
Practical Usages of key_exists() Function:
The function is useful when you want to make sure that an array contains a specific key before accessing its value to avoid errors. Check example 2.
Best Practices on key_exists() Function:
Before accessing an array key, use this function else you’ll get an “Undefined array key” error. Check example 3.
Notes on key_exists() Function:
- The function can work on both indexed or associative array. Check example 4.
- The function can differentiate uppercase and lowercase keys.
<?php $arr = array("one"=>"PHP", "One"=>"PHP 8.3", "two"=>"Python", "three"=>"Java"); echo $arr["one"] . " - " .$arr["One"]; echo "<br />"; if(!key_exists("ONE", $arr)) echo "Key doesn't exist."; ?>
Output:
PHP - PHP 8.3 Key doesn't exist.
Explanation:
The key_exists() function consider the keys “one” and “One” two different keys. For the same reason, the function doesn’t consider the key “ONE” exists in the array.
- It is an alias of array_key_exists() function.
Caution:
- The return value TRUE of this function doesn’t mean that the key contains a value that isset() function considers as TRUE. If an array value is NULL, the key_exists() function returns TRUE if you search for it, but the isset() function considers this variable as FALSE. Have a look at the example below-
<?php $arr = array("one", "two", "three", NULL); echo key_exists(3, $arr)? "Key exists.":"Key doesn't exist."; echo "<br />"; if(isset($arr[3])) echo "key exists."; else echo "key doesn't exists."; ?>
Output:
Key exists. key doesn't exists.
Explanation:
The key_exists() function returns TRUE if an element contains NULL value (Line 3). On the other hand, the isset() function considers the element doesn’t have any value. So, it returns FALSE.
- The function doesn’t return TRUE even though the key exists in the array if the key resides inside the inner array. That means, the function doesn’t consider keys of the inner array of multidimensional array. Check the example below-
<?php // #5 $arr = array("one"=>"JS", "client"=>array("two"=>"HTML", "three"=>"CSS"), "four"=>"PHP", "server"=>array("five"=>"Python", "six"=>"Java")); echo key_exists("one", $arr)? "Key exists.":"Key doesn't exist."; echo "<br />"; echo key_exists("two", $arr)? "Key exists.":"Key doesn't exist."; echo "<br />"; ?>
Output:
Key exists. Key doesn't exist.
Explanation:
The key “two” exists in the array, but the function returns ” Key doesn’t exist.” because the key is in the inner array and the key_exists() function only works in outer most array.
- As this function rounds the float key down to integer you may get a different result. Look the example below-
<?php $arr = array(1 => 10, 1.6 => "A", 2.5 => "B", 3.5 => "C"); var_dump($arr[1]); ?>
Output:
string(1) "A"
Explanation:
The key 1.6 becomes 1 and then, there are two keys named “1”. And, in this case the later key is taken.
PHP Version Support:
PHP 4, PHP 5, PHP 7, PHP 8
Summary: PHP key_exists() Function
You’ll often use this built-in array function. Use key_exists() function when you just want to when whether a specific key exists in an array regardless of its value. You don’t care about the type of the value.