PHP array_key_exists() Function

The array_key_exists() function is used to check whether a specified key is present in an array or not. The function returns TRUE if the given key is set in the array. The key can be any value possible for an array index.

Tip: Remember that if you skip the key when you specify an array, an integer key is generated, starting at 0 and increases by 1 for each value.

ParameterDescription
keyRequired. Specifies the key
arrayRequired. Specifies an array

Syntax

 array_key_exists(key,array) 

Example

$array=array("a"=>"Cooper","b"=>"James");
if (array_key_exists("c",$array))
{
	echo "Key exists!";
}
else
{
	echo "Key does not exist!";
}

Output

Key does not exist!

More Example

$array=array("Cooper","James");
if (array_key_exists(0,$array))
{
	echo "Key exists!";
}
else
{
	echo "Key does not exist!";
}

Output

Key exists!

Leave a Reply

Your email address will not be published. Required fields are marked *