php

PHP array_key_exists() Function

If you want to check if a particular key is present in the array or not, you can use PHP. Then we can directly use the array_key_exists() function.

The array_key_exists() function determines whether or not the specified key exists in an array. If the key exists, it returns 1, otherwise empty (nothing) is returned. It takes two parameters.

Syntax:
array_key_exists(key,array_input)

Parameters:

  1. The key specifies the key name.
  2. Array_input is the array.

Let’s see the examples to understand this concept better.

Example 1:
In this example, we will create an array: Flower that has 4 key-value pairs.
‘flower_name’=>’lotus’,’sepal’=>4,’petal’=>3,’area’=>’water’.

After that, we will check if the key-flower_name exists or not.

<?php

//create an array named Flower1 with 4 key-values
$Flower=array('flower_name'=>'lotus','sepal'=>4,'petal'=>3,'area'=>'water');

print_r("Actual array:");
print_r($Flower);

//check the key-flower_name exists in Flower or not.
print("Does flower_name exists?:  ");
print_r(array_key_exists("flower_name",$Flower));
?>

Output:

We can see that the array_key_exists() function returned 1 since the key flower_name exists.

Example 2:
In this example, we will create an array: Flower that has 4 key-value pairs. ‘flower_name’=>’lotus’,’sepal’=>4,’petal’=>3,’area’=>’water’.

After that, we will check if the key-flower_id exists or not.

<?php

//create an array named Flower1 with 4 key-values
$Flower=array('flower_name'=>'lotus','sepal'=>4,'petal'=>3,'area'=>'water');

print_r("Actual array:");
print_r($Flower);

//check the key-flower_id exists in Flower or not.
print("Does flower_id exists?:  ");
print_r(array_key_exists("flower_id",$Flower));
?>

Output:

We can see that the array_key_exists() function returned empty (nothing) since the key flower_id doesn’t exist.

If there are only values in the input array, then by default the keys will be 0,1,2,3…
For example, if there are 4 values in an array, then the keys are 0,1,2, and 3.

Example 3:
In this example, we will create an array Flower with 4 values: ‘lotus’,4,3,’water’ and check if the keys -0,1,2,3 exist or not.

<?php

//create an array named Flower1 with values
$Flower=array('lotus',4,3,'water');

print_r("Actual array:");
print_r($Flower);

//check the key-0 exists in Flower or not.
echo("Does 0 exists?: ".array_key_exists(0,$Flower)."\n");

//check if the key-1 exists in Flower or not.
echo("Does 1 exists?: ".array_key_exists(1,$Flower)."\n");

//check if the key-2 exists in Flower or not.
echo("Does 2 exists?: ".array_key_exists(2,$Flower)."\n");

//check if the key-3 exists in Flower or not.
echo("Does 3 exists?: ".array_key_exists(3,$Flower));

?>

Output:

We can see that the keys exist in the array. It is also possible to use the array_key_exists() function inside the if condition.

Example 4:
In this example, we will check if keys -0 and 6 exist in the given array or do not use if-else statements.

<?php

//create an array named Flower1 with values
$Flower=array('lotus',4,3,'water');

print_r("Actual array:");
print_r($Flower);

//check the key-0 exists in Flower or not.
if(array_key_exists(0,$Flower)){
   
    print("key-0 exists in the given array");
}
else{
        print("key-0 not exists in the given array");

}
echo "\n";
//check whether the key-6 exists in Flower or not.
if(array_key_exists(6,$Flower)){
   
    print("key-6 exists in the given array");
}
else{
        print("key-6 not exists in the given array");

}

?>

Output:

First, if the block is executed since key-0 is present in the given array, the second block is executed since key-6 does not exist in the array.

Conclusion

In this tutorial, we discussed how to check whether the given key exists in the array or not using the array_key_exists() function. It will return1 if the key is present in the array, otherwise, it will return empty. We also implemented this function inside if-else statements.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain