array_key_exists function in php
Last Updated by Pooja Chikara
0 2700
array_key_exists function in PHP used find whether a particular key or index is exist in array or not.
- It is an inbuilt function of PHP with two parameters.
- Return type of array_key_exists function is Boolean means it can return either TRUE or FALSE.
- If the given key is present in the array , then array_key_exists function returns TRUE Otherwise it returns FALSE.
Syntax:
array_key_exists(arr_key, arr);
Here
arr is the array in which we search the existence of a particular key and arr_key parameter represent that key which we want to search.
Example 1:
<?php
$arr=array("Ram"=>"1231","Shyam"=>"1232","Deepak"=>"1233","Divya"=>"1234","Radha"=>"1235"); // employees name with ID
$key="Radha";
if(array_key_exists($key,$arr)){
echo "The employee ID of ".$key." is ".$arr[$key];
}else{
echo $key." is not exist";
}
?>Output:

Example 2:
<?php
$arr=array("Ram","Shyam","Deepak","Divya","Radha"); // employees name
if(array_key_exists(3,$arr)){
echo "The employee ".$arr[3]." is exist";
}else{
echo "not exist";
}
?>Output:

Share:

Comments
Waiting for your comments