Verify if the key “Volvo” is present in an 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 if a specified key is present in an array, returning true if the key exists and false if it does not.
Tip: If you do not specify a key when defining an array, integer keys are automatically generated, starting from 0 and incrementing by 1 for each value.
array_key_exists(key, array) |
Parameter |
Description |
key |
Required. Specifies the key to check for. |
array |
Required. Specifies the array to search. |
Return Value: |
Returns TRUE if the key exists and FALSE if it does not. |
PHP Version: |
4.0.7+ |
Check if the key “Toyota” is present in an array.
<?php $a=array(“Volvo”=>“XC90”,“BMW”=>“X5”); if (array_key_exists(“Toyota”,$a)) { echo “Key exists!”; } else { echo “Key does not exist!”; } ?> |
Check if the integer key “0” exists in an array.
<?php $a=array(“Volvo”,“BMW”); if (array_key_exists(0,$a)) { echo “Key exists!”; } else { echo “Key does not exist!”; } ?> |