Check whether the key "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!"; }?>The array_key_exists() function checks whether the specified key exists in an array. It returns true if the key exists and false if the key does not exist.
Tip: Remember that if you omit the keys when specifying an array, integer keys will be generated starting at 0 and increasing by 1. (See Example 2)
array_key_exists( key,array )
parameter | describe |
---|---|
key | Required. Specifies the key name. |
array | Required. Specifies an array. |
Return value: | Returns TRUE if the key exists, and FALSE if the key does not exist. |
---|---|
PHP version: | 4.0.7+ |
Check if the key "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!"; }?>Check if the integer key "0" exists in the array:
<?php$a=array("Volvo","BMW");if (array_key_exists(0,$a)) { echo "Key exists!"; }else { echo "Key does not exist!"; }?>