PHP array_key_exists () function
Examples
Check the key name "Volvo" exists in the array:
<?php
$a=array("Volvo"=>"XC90","BMW"=>"X5");
if (array_key_exists("Volvo",$a))
{
echo "Key exists!";
}
else
{
echo "Key does not exist!";
}
?>
$a=array("Volvo"=>"XC90","BMW"=>"X5");
if (array_key_exists("Volvo",$a))
{
echo "Key exists!";
}
else
{
echo "Key does not exist!";
}
?>
Running instance »
Definition and Usage
array_key_exists () function checks whether there is an array with a specified key, if the key name exists returns true, if the key does not exist it returns false.
Tip: Remember, if you specify when the array is omitted key name will be generated from 0 to 1 and incrementing integer key.(See Example 2)
grammar
array_key_exists( key,array )
参数 | 描述 |
---|---|
key | 必需。规定键名。 |
array | 必需。规定数组。 |
technical details
return value: | If the key name exists returns TRUE, if the key does not exist it returns FALSE. |
---|---|
PHP version: | 4.0.7+ |
More examples
Example 1
Check the key name "Toyota" exists in the array:
<?php
$a=array("Volvo"=>"XC90","BMW"=>"X5");
if (key_exists("Toyota",$a))
{
echo "Key exists!";
}
else
{
echo "Key does not exist!";
}
?>
$a=array("Volvo"=>"XC90","BMW"=>"X5");
if (key_exists("Toyota",$a))
{
echo "Key exists!";
}
else
{
echo "Key does not exist!";
}
?>
Running instance »
Example 2
Check integer key "0" is present in the array:
<?php
$a=array("Volvo","BMW");
if (array_key_exists(0,$a))
{
echo "Key exists!";
}
else
{
echo "Key does not exist!";
}
?>
$a=array("Volvo","BMW");
if (array_key_exists(0,$a))
{
echo "Key exists!";
}
else
{
echo "Key does not exist!";
}
?>
Running instance »
Complete PHP Array Reference