PHP array_key_exists() Function
Definition
The array_key_exists() function checks existence of a specified key in an array.
Syntax
PHP array_key_exists() Function has the following syntax.
array_key_exists(key,array)
Parameter
Parameter | Is Required | Description |
---|---|---|
key | Required. | Key to look for |
array | Required. | Array to look for |
Return
PHP array_key_exists() Function returns true if the key exists and false if the key does not exist.
Example 1
Check if the key "j" exists in an array:
<?php//from w w w . j a v a2 s . co m
$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.
Example 2
Check if the integer key "0" exists in an array:
<?php/*w w w. ja v a2s . co m*/
$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.