PHP key() Function
last modified March 13, 2025
The PHP key
function fetches the key of the current array element.
It's part of PHP's array pointer functions, working with the internal pointer.
Basic Definition
The key
function returns the key of the element currently pointed
to by the internal array pointer. It doesn't advance the pointer.
Syntax: key(array $array): mixed
. Returns the current key or null
if the pointer is beyond array bounds. Works with both indexed and associative.
Basic key() Example
This demonstrates getting the current key from a simple associative array.
<?php $colors = [ 'red' => '#FF0000', 'green' => '#00FF00', 'blue' => '#0000FF' ]; $currentKey = key($colors); echo "Current key: $currentKey";
The internal pointer starts at the first element. key
returns
'red' as it's the key of the first element. The pointer remains unchanged.
Using key() in a Loop
Combine key
with next
to iterate through keys.
<?php $fruits = [ 'apple' => 'red', 'banana' => 'yellow', 'grape' => 'purple' ]; while ($key = key($fruits)) { echo "Key: $key, Value: {$fruits[$key]}\n"; next($fruits); }
This prints all key-value pairs. key
gets the current key,
while next
advances the pointer. The loop stops when key()
returns null.
key() with Indexed Arrays
key
works with numeric indices just like associative keys.
<?php $numbers = [10, 20, 30, 40]; echo "First key: " . key($numbers) . "\n"; next($numbers); echo "Second key: " . key($numbers);
For indexed arrays, key
returns the numeric index. After
next
, it moves to the next index (1) in this example.
key() After Array Modification
Array modifications can affect the internal pointer position.
<?php $data = [ 'a' => 1, 'b' => 2, 'c' => 3 ]; next($data); // Move to 'b' unset($data['b']); // Remove current element echo "Current key after removal: " . key($data);
When removing the current element, PHP moves the pointer to the next element.
Here, after removing 'b', the pointer moves to 'c', which key
then returns.
key() with Empty Arrays
key
returns null when used on empty arrays or at array end.
<?php $empty = []; $end = ['a', 'b']; end($end); // Move past last element var_dump(key($empty)); var_dump(key($end));
For empty arrays or when the pointer is beyond array bounds, key
returns null. This behavior helps detect when iteration should stop.
Best Practices
- Reset Pointer: Use
reset
before iteration. - Check Validity: Verify return value isn't null.
- Combine Functions: Pair with
current
. - Avoid Modification: Don't modify arrays during iteration.
Source
This tutorial covered the PHP key
function with practical
examples showing its usage for array key retrieval scenarios.
Author
List all PHP Array Functions.