PHP array_key_exists() function
The array_key_exists () function in PHP is used to check whether the specified key exists in the array or not. It returns a Boolean value TRUE if the given key exists else it returns FALSE.
Syntax
array_key_exists(mixed key,array$array)
Parameter
key(required)- This parameter signifies the value to check.
array(required)- This parameter signifies the input array to check.
Return
This function returns a Boolean value TRUE on success or FALSE on failure.
Example 1
'first', 2=>'second',3 => 'third',4=>'fourth');
// passing the key value
$key= 3;
// will check whether the key exists or not
if (array_key_exists($key, $search_array)) {
echo "The 3 key element is present in the array";
}
?>
Output
The 2 key element is present in the array
Example 2
'first', 2=>'second',3 => null, 4=>'fourth');
// passing the key value
$key= 3;
// will check whether the key exists or not
if (array_key_exists($key, $search_array)) {
echo "The ".$key." key element is present in the array";
}
else {
echo "The ".$key." key element is not present in the array";
}
?>
Output
The 3 key element is present in the array
Example 3
Output
The 3 key element is not present in the array
