php8.5
Home/ Manual/ array / functions/ array_key_exists

array_key_exists

PHP function Edit on GitHub ✎

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

Checks if the given key or index exists in the array

Description

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

array_key_exists() returns true if the given key is set in the array. key can be any value possible for an array index.

Parameters

key

Value to check.

array

An array with keys to check.

Return Values

Success

Note

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

Changelog

VersionDescription
8.5.0Using null in the key parameter is deprecated, use an empty string instead.
8.0.0The key parameter now accepts bool, float, int, null, resource, and string as arguments.
8.0.0Passing an object to the array parameter is no longer supported.
7.4.0Passing an object to the array parameter has been deprecated. Use property_exists() instead.

Examples

array_key_exists() example

php
<?php
$searchArray = ['first' => 1, 'second' => 4];
var_dump(array_key_exists('first', $searchArray));
?>

The above example will output:

output
bool(true)

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
<?php
$searchArray = ['first' => null, 'second' => 4];

var_dump(isset($searchArray['first']));
var_dump(array_key_exists('first', $searchArray));
?>

The above example will output:

output
bool(false)
bool(true)

See Also

Source: reference/array/functions/array-key-exists.xml · from the official PHP manual (php/doc-en)