PHP array_key_exists Function
last modified March 13, 2025
The PHP array_key_exists
function checks if a specified key exists
in an array. It's essential for safe array access and validation.
Basic Definition
The array_key_exists
function verifies if a key is present in an
array. It returns true if the key exists, false otherwise.
Syntax: array_key_exists(string|int $key, array $array): bool
. The
function works with both string and integer keys in any array type.
Basic array_key_exists Example
This demonstrates checking for a key in a simple associative array.
<?php $user = [ 'name' => 'John', 'age' => 30, 'email' => 'john@example.com' ]; if (array_key_exists('email', $user)) { echo "Email exists: {$user['email']}"; } else { echo "Email not found"; }
This checks if the 'email' key exists in the $user array. Since it does, the code outputs the email address. Always validate keys before access.
Checking Numeric Keys
The function works equally well with numeric array indices.
<?php $colors = ['red', 'green', 'blue']; if (array_key_exists(1, $colors)) { echo "Second color is: {$colors[1]}"; } if (!array_key_exists(3, $colors)) { echo "Index 3 doesn't exist"; }
This verifies numeric indices in a sequential array. Note that PHP arrays are zero-indexed, so index 1 refers to the second element.
Difference from isset()
array_key_exists
differs from isset
in handling null values.
<?php $data = [ 'name' => 'Alice', 'age' => null ]; var_dump(array_key_exists('age', $data)); // bool(true) var_dump(isset($data['age'])); // bool(false)
array_key_exists
returns true for null values, while isset
returns false. Choose based on whether you need to detect null as valid.
Multi-dimensional Arrays
The function can check keys in nested array structures.
<?php $inventory = [ 'fruits' => [ 'apple' => 10, 'banana' => 15 ], 'vegetables' => [ 'carrot' => 20 ] ]; if (array_key_exists('fruits', $inventory) && array_key_exists('banana', $inventory['fruits'])) { echo "Banana count: {$inventory['fruits']['banana']}"; }
This checks keys at multiple levels. First verify the parent key exists, then check the nested key to avoid undefined index errors.
Performance Considerations
The function has constant time complexity (O(1)) for hash table lookups.
<?php $largeArray = array_fill(0, 1000000, 'value'); $start = microtime(true); array_key_exists(999999, $largeArray); $time = microtime(true) - $start; echo "Key check took: " . number_format($time * 1000, 3) . " ms";
Even with one million elements, key lookup remains fast. The function uses PHP's internal hash table implementation for efficient searches.
Best Practices
- Pre-validation: Use before accessing array elements.
- Type safety: Works with both string and integer keys.
- Null handling: Prefer over isset() when null is valid.
- Readability: Makes code intentions clear.
Source
PHP array_key_exists Documentation
This tutorial covered the PHP array_key_exists
function with
practical examples showing its usage for array key validation.
Author
List all PHP Array Functions.