array_key_first()
array_key_first() — Gets the first key of an array.
Built-in function of PHP 8.5. The manual lists it as available in PHP 7 >= 7.3.0, 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
Get the first key of the given array without affecting the internal array pointer.
Signature
array_key_first(array $array): int|string|null
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $array | array | An array. |
Return value
Returns the first key of array if the array is not empty; null otherwise.
Example
A runnable snippet:
$array = ['a' => 1, 'b' => 2, 'c' => 3]; $firstKey = array_key_first($array); var_dump($firstKey);
Output:
string(1) "a"
See also
array_first(), array_key_last(), reset()
All PHP 8.5 built-in functions
Reference card built from the PHP manual page array_key_first() (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.