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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

array_key_first(array $array): int|string|null

Parameters

ParameterDescription
array The input array.

Return value

The first key, or null.

Examples

First keyTry it yourself
<?php
$scores = ["ann" => 90, "bob" => 72];
echo array_key_first($scores), "\n";
var_dump(array_key_first([]));

Output

ann
NULL
First valueTry it yourself
<?php
$list = [5 => "x", 9 => "y"];
echo $list[array_key_first($list)], "\n";

Output

x

Version history

VersionChange
PHP 7.3Added

Was this page helpful?