Instance
Check to see if 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
The array_key_exists () function checks to see if the specified key name exists in an array, returns true if the key name exists, or False if the key name does not exist.
Tip: Keep in mind that if you omit the key name when you specify an array, an integer key name that starts at 0 and increments by 1 is generated. (see Example 2)
Grammar
Array_key_exists (Key,array)
Parameters |
Describe |
Key |
Necessary. Specifies the key name. |
Array |
Necessary. Specifies the array. |
Technical details
return value: |
Returns TRUE if the key name exists, or FALSE if the key name does not exist. |
PHP version: |
4.0.7+ |
More examples
Example 1
Check that the key name "Toyota" is present 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 that the integer key name "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!";}? >
Example 1
<?php $a =array ("a" and "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" and "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!