PHP array_key_exists – checking a key exist in array



PHP array_key_exists function is used for checking a specified key or index exists in an array. It will return TRUE if the specified key exists in array or it will return FALSE if that key is not present in the array.Its using for a key search in an array. We don’t need to use loop through complete array and check each time with the key.Just need to use this function for this purpose.

Its Syntax will be :
array_key_exists function( Key value to search , array )

Now look at a sample PHP program with array_key_exists function for searching a key in array

<?php
$age = array("Syam"=>30, "Binu"=>25, "Bob"=>50, "cathy"=>30);
$key_search="Bob";
if (array_key_exists($key_search,$age))
{
echo "KEY EXISTS IN ARRAY";
}
else
{
echo "KEY CANNOT BE FOUND";
}
?>

Its output will be :

KEY EXISTS IN ARRAY

In this example our key is “Bob” and that key is there in the declared array. So our
array_key_exists() function will return TRUE in this example.