In this tutorial, we will learn how to check whether the given key exists in the array or not using the array_key_exists() function or the array key exists in PHP. 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.
The PHP array key exists function is used to check in php if an array key exists or 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.
PHP array_key_exists() Syntax
boolean array_key_exists ($key, $array)
$key: This argument refers to the key that you want to search in a given array.
$array: This argument refers to the array in which you want to search the given key β$keyβ.
Return value: This function will return the boolean type value βTrueβ or βFalseβ. It depends, on whether the key exists in the array or not.
Example :
In this example, we will create an array: Flower that has 4 key-value pairs.
βflower_nameβ=>βlotusβ,βsepalβ=>4,βpetalβ=>3,βareaβ=>βwaterβ.
<?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 :
Actual array:Array
{
[flower_name] => lotus
[sepal] => 4
[petal] => 3
[area] => water
}
Does flower_name exists?:
Example :
<?php
// PHP function to illustrate the use
// of array_key_exists()
function Exists($index, $array)
{
if (array_key_exists($index, $array))
{
echo "Found the Key";
}
else
{
echo "Key not Found";
}
}
$array = array(
"ram" => 25,
"krishna" => 10,
"aakash" => 20,
"gaurav"
);
$index = "aakash";
print_r(Exists($index, $array));
?>
Output :
Found the Key
If no key_value pair exits, as shown in the below case, then the array takes the default keys i.e. numeric keys starting from zero, into consideration and returns true as far as the $index limit ranges the Key
Parameters :
PHP array_key_exists() is an inbuilt function in PHP and is used to check whether a specific key or index exists in the array or not. This function returns a boolean type value βTrueβ or βFalseβ. If specified key is found in the array, return the true otherwise false. To learn more about PHP Array Functions.
PHP Associative Array
<?php
$arr = array("naresh" => 15, "ankur" => 18);
$key = "naresh";
if (array_key_exists($key,$arr))
{
echo "Wow ! Key exists in Array";
}
else
{
echo "Key does not exist!";
}
?>
Output :
Wow ! Key exists in Array
Example :
<?php
$arr = array("james" => 15, "david" => 18);
$key = "smith";
if (array_key_exists($key,$arr))
{
echo "Wow ! Key exists in Array";
}
else
{
echo "Key does not exist!";
}
?>
Output :
Key does not exist!
Indexed Array PHP
<?php
$arr = array("david","james","john","smith");
$key = 1;
if (array_key_exists($key,$arr))
{
echo "Wow ! Key exists in Array";
}
else
{
echo "Key does not exist!";
}
?>
Output :
Wow ! Key exists in Array
Example :
<?php
$arr = array("david","james","john","smith");
$key = 4;
if (array_key_exists($key,$arr))
{
echo "Wow ! Key exists in Array";
}
else
{
echo "Key does not exist!";
}
?>
Output :
Key does not exist!
More Related Post
Thanks for the reading post. I hope you like and understand the post. If you have any doubts regarding this post please comment below.