How do you check if a key exists in an array php?

(PHP 4 >= 4.0.7, PHP 5, PHP 7, PHP 8)

array_key_existsChecks if the given key or index exists in the array

Description

array_key_exists(string|int $key, array $array): bool

Parameters

key

Value to check.

array

An array with keys to check.

Return Values

Returns true on success or false on failure.

Note:

array_key_exists() will search for the keys in the first dimension only. Nested keys in multidimensional arrays will not be found.

Examples

Example #1 array_key_exists() example

$search_array = array('first' => 1'second' => 4);
if (
array_key_exists('first'$search_array)) {
    echo 
"The 'first' element is in the array";
}
?>

Example #2 array_key_exists() vs isset()

isset() does not return true for array keys that correspond to a null value, while array_key_exists() does.

$search_array = array('first' => null'second' => 4);// returns false
isset($search_array['first']);// returns true
array_key_exists('first'$search_array);
?>

Notes

Note:

For backward compatibility reasons, array_key_exists() will also return true if key is a property defined within an object given as array. This behaviour is deprecated as of PHP 7.4.0, and removed as of PHP 8.0.0.

To check whether a property exists in an object, property_exists() should be used.

See Also

  • isset() - Determine if a variable is declared and is different than null
  • array_keys() - Return all the keys or a subset of the keys of an array
  • in_array() - Checks if a value exists in an array
  • property_exists() - Checks if the object or class has a property

manhon824 at gmail dot com

11 years ago

I took hours for me to debug, and I finally recognized that,

You have to reset the $array before using array_key_exists
reset($array);
array_key_exists($needle,$array);

Or you will get no reply.

❮ PHP Array Reference

Example

Check if the key "Volvo" exists in an array:

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

Try it Yourself »


Definition and Usage

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

Tip: Remember that if you skip the key when you specify an array, an integer key is generated, starting at 0 and increases by 1 for each value. (See example below)


Syntax

array_key_exists(key, array)

Parameter Values

ParameterDescription
key Required. Specifies the key
array Required. Specifies an array


Technical Details

Return Value:Returns TRUE if the key exists and FALSE if the key does not exist
PHP Version:4.0.7+

More Examples

Example

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

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

Try it Yourself »

Example

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

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

Try it Yourself »


❮ PHP Array Reference


How do you find the key in an array?

You can use array_keys() to get ALL the keys of an array, e.g.

What is array_keys () used for in PHP?

The array_keys() function returns an array containing the keys.

How do you check if an index is present in an array?

indexOf() The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

How do you check if a key exists in an array JavaScript?

Using the indexOf() Method JavaScript's indexOf() method will return the index of the first instance of an element in the array. If the element does not exist then, -1 is returned.