Home >>PHP Array Functions >PHP array_key_exists() Function

PHP array_key_exists() Function

PHP array_key_exists() Function

PHP array_key_exists() function is used to check whether a specific key or index is present inside any given array or not. It returns True if the specified key is found in the array otherwise it returns false.It accepts two arguments $key and $array. It returns a boolean value as result.

Syntax:

array_key_exists($key, $array);

Parameter Values

Parameter Description
key This is a required parameter. This parameter defines the key.
array This is a required parameter. This parameter defines an array.

Here is an example of array_key_exists() function in PHP:

<html>
<body>
<?php
$x=array("Name"=>"Jerry","Age"=>"21","Place"=>"Noida","Project"=>"PHP");
if (array_key_exists("Project",$x))
  {
  echo "Key exists!";
  }
else
  {
  echo "Key does not exist!";
  }
?>
</body>
</html>
Output:
Key exists!

Example 2:

<html>
<body>
<?php
$x=array("Name"=>"Jerry","Age"=>"21","Place"=>"Noida","Project"=>"PHP");
if (array_key_exists("Project",$x))
  {
  echo "Project Key exists!<br>";
  }
else
  {
  echo "Project Key does not exist!<br>";
  }
if (array_key_exists("Salary",$x))
  {
  echo "Salary Key exists!";
  }
else
  {
  echo "Salary Key does not exist!";
  }
?>
</body>
</html>
Output:
Project Key exists!
Salary Key does not exist!

No Sidebar ads