PHP 8 reference · Array Functions
PHP array_key_first() Function
Return the first key of an array without moving its internal pointer, or null for an empty array. Added in 7.3.
PHP 8 Since 7.3
Syntax
array_key_first(array $array): int|string|null
Parameters
| Parameter | Description |
|---|---|
array |
The input array. |
Return value
The first key, or null.
Examples
<?php
$scores = ["ann" => 90, "bob" => 72];
echo array_key_first($scores), "\n";
var_dump(array_key_first([]));
Output
ann NULL
<?php
$list = [5 => "x", 9 => "y"];
echo $list[array_key_first($list)], "\n";
Output
x
Version history
| Version | Change |
|---|---|
| PHP 7.3 | Added |
Related
array_key_last()· Return the last key of an array without moving its internal pointer, or null for an empty array. Added in 7.3.array_first()· Return the first value of an array, or null if it's empty, without touching the internal pointer. Added in PHP 8.5.- PHP Arrays · tutorial
- PHP Array Functions · tutorial