PHP array_key_exists()Functions

PHP Array ReferenceComplete PHP Array Reference Manual

Examples

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!"; } ?>

Run Example »

Definition and Usage

The array_key_exists() function checks whether a 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:Remember, if you omit the key name when specifying an array, integer key names will be generated starting from 0 and incrementing by 1. (See Example 2)


Syntax

array_key_exists(key,array)

Parameter Description
key Required. Specifies the key name.
array Required. Specifies the array.

Technical Details

Return value: Returns TRUE if the key name exists, and FALSE if the key name does not exist.
PHP Version: 4.0.7+


More Examples

Example 1

Check whether the key name "Toyota" exists in the array:

<?php $a=array("Volvo"=>"XC90","BMW"=>"X5"); if (array_key_exists("Toyota",$a)) { echo "Key exists!"; } else { echo "Key does not exist!"; } ?>

Run Example »

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!"; } ?>

Run Example »


PHP Array ReferenceComplete PHP Array Reference Manual

Other Extensions