Check key exist in 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

<?php
$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.

<?php
$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:

<?php
$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:

<?php
$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:

<?php
$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 check if a key exists in a PHP array?

Answer: Use the PHP array_key_exists() function You can use the PHP array_key_exists() function to test whether a given key or index exists in an array or not. This function returns TRUE on success or FALSE on failure.

Does key exist in array?

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.

How do you find the key in an array?

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

How do you check if an index exists in an array PHP?

PHP: Checks if the given key or index exists in an array The array_key_exists() function is used to check whether a specified key is present in an array or not. The function returns TRUE if the given key is set in the array. The key can be any value possible for an array index.