The array_key_exists() function checks existence of a specified key in an array.
PHP array_key_exists() Function has the following syntax.
array_key_exists(key,array)
Parameter | Is Required | Description |
---|---|---|
key | Required. | Key to look for |
array | Required. | Array to look for |
PHP array_key_exists() Function returns true if the key exists and false if the key does not exist.
Check if the key "j" exists in an array:
<?php
$a=array("a"=>"A","b"=>"Bed","c"=>"Cat","j"=>"java2s.com");
if (array_key_exists("j",$a)){
echo "Key exists!";
}else{
echo "Key does not exist!";
}
?>
The code above generates the following result.
Check if the integer key "0" exists in an array:
<?php
$a=array("PHP","Java");
if (array_key_exists(0,$a)){
echo "Key exists!";
}else{
echo "Key does not exist!";
}
?>
The code above generates the following result.