array_key_exists()
array_key_exists() — Checks if the given key or index exists in the array.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4 >= 4.0.7, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
array_key_exists() returns true if the given key is set in the array. key can be any value possible for an array index.
Signature
array_key_exists(string|int|float|bool|resource|null $key, array $array): bool
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $key | string|int|float|bool|resource|null | Value to check. |
| $array | array | An array with keys to check. |
Return value
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. array_key_exists() will search for the keys in the first dimension only. Nested keys in multidimensional arrays will not be found.
Example
A runnable snippet:
$searchArray = ['first' => 1, 'second' => 4]; var_dump(array_key_exists('first', $searchArray));
Output:
bool(true)
Changelog
| Version | Change |
|---|---|
| 8.5.0 | Using null in the key parameter is deprecated, use an empty string instead. |
| 8.0.0 | The key parameter now accepts bool, float, int, null, resource, and string as arguments. |
| 8.0.0 | Passing an object to the array parameter is no longer supported. |
| 7.4.0 | Passing an object to the array parameter has been deprecated. Use property_exists() instead. |
See also
array_keys(), in_array(), property_exists()
All PHP 8.5 built-in functions
Reference card built from the PHP manual page array_key_exists() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.