Current Location: Home> Function Categories> array_key_exists

array_key_exists

Check whether there is a specified key name or index in the array
Name:array_key_exists
Category:Array
Programming Language:php
One-line Description:Checks whether the specified key name exists in the array.

Definition and usage

array_key_exists() function checks whether the 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 an array, an integer key name starting from 0 and each key value is incremented by 1. (See Example 2)

Example

Example 1

Check whether the key name "Volvo" exists in the array:

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

Try it yourself

Example 2

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

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

Try it yourself

Example 3

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

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

Try it yourself

grammar

 array_key_exists ( key , array )
parameter describe
key Required. Specify key names.
array Required. Specify array.
Similar Functions