Course Recommendations:

PHP array_key_exists() Function

Example

<?php
$a=array("Volvo"=>"XC90","BMW"=>"X5");
if (Check if the key name "Volvo" exists in the array:)
  {
  echo "Key exists!";
  }
else
  {
  echo "Key does not exist!";
  }
?>

Run Instances

Definition and Usage

The array_key_exists() function checks if a specified key name exists in an array, returns true if the key name exists, and returns false if the key name does not exist.

Tip:Remember, if you omit the key name when specifying the array, an array starting from 0 and each key value corresponding to an increment of 1 will be generated. (See example 2)

Syntax

array_key_exists(key,array)
Parameters Description
key Required. Specifies the key name.
array Required. Specifies the array.

Technical Details

Return Value: Returns TRUE if the key name exists, otherwise FALSE.
PHP Version: 4.0.7+

More Examples

Example 1

Check if the key name "Toyota" exists in the array:

<?php
$a=array("Volvo"=>"XC90","BMW"=>"X5");
if (key_exists("Toyota",$a))
  {
  echo "Key exists!";
  }
else
  {
  echo "Key does not exist!";
  }
?>

Run Instances

Example 2

Check if the integer key name "0" exists in the array:

<?php
$a=array("Volvo","BMW");
if (array_key_exists(0,$a))
  {
  echo "Key exists!";
  }
else
  {
  echo "Key does not exist!";
  }
?>

Run Instances