Example
Check whether 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!"; } ?>
Definition and usage
array_key_exists() Function Checks whether the specified key name exists in an array. If the key name exists, it returns true. If the key name does not exist, it returns false.
Tip: Please remember that if you omit the key name when specifying the array, an integerkey name starting from 0 and increasing by 1 will be generated. (See Example 2)
Syntax
array_key_exists(key,array)
Parameters | Description |
key | Required. Specifies the key name. |
array | Required. Specifies an array. |
Technical details
Return value: | Returns TRUE if the key name exists, if If the key name does not exist, FALSE is returned. |
PHP version: | 4.0.7+ |
More examples
Examples 1
Check whether 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!"; } ?>
Example 2
Check whether the integer key name "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!"; } ?>
Example 1
<?php $a=array("a"=>"Dog","b"=>"Cat"); if (array_key_exists("a",$a)) { echo "Key exists!"; } else { echo "Key does not exist!"; } ?>
Output:
Key exists!
Example 2
<?php $a=array("a"=>"Dog","b"=>"Cat"); if (array_key_exists("c",$a)) { echo "Key exists!"; } else { echo "Key does not exist!"; } ?>
Output:
Key does not exist!
Example 2
<?php $a=array("Dog",Cat"); if (array_key_exists(0,$a)) { echo "Key exists!"; } else { echo "Key does not exist!"; } ?>
Output:
Key exists!
The above is the detailed content of PHP function array_key_exists() that checks whether the specified key exists in the array. For more information, please follow other related articles on the PHP Chinese website!