The array_key_exists function checks if the specified key exists in the array. If the key exists, it returns true, false in case of the key is not found.
What is the syntax of the array_key_exists function in PHP?
array_key_exists(key, array)
Parameter Values
Parameters | Details |
---|---|
key | Required. Specifies the key |
array | Required. Specifies an array |
Example of array_key_exists function
<?php
$a=array("Honda"=>"XC90","BMW"=>"X5");
if (array_key_exists("Honda",$a))
{
echo "Key exists!";
}
else
{
echo "Key does not exist!";
}
?>
In the above example, the array_key_exists checks for the “Honda” key in the array.
<?php
$a=array("Volvo","BMW");
if (array_key_exists(0,$a))
{
echo "Key exists!";
}
else
{
echo "Key does not exist!";
}
?>
In the above example, the name (string) of the key is not specified in the function. In this case, when no string is specified to the array_key_exists function, it assumes the integer key that starts from 0 and increments for every key in the specified array.