PHP array_key_first() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_key_first() returns the first key of an array without changing the array.

It returns null for an empty array. This replaces the older trick of calling reset() and key().

Syntax

Call array_key_first() with the values you need. Start with this line.

PHP
$firstKey = array_key_first($items);
CallWhat it does
array_key_first(["x" => 1, "y" => 2])Returns "x".
array_key_first([5 => "a", 2 => "b"])Returns 5, because that key was added first.
array_key_first([])Returns null for an empty array.

The result is the first key in the array's current order, not the smallest key.

Library declarations
PHP
array_key_first(array $array): int|string|null

Parameters

ParameterDescription
arrayRequired. The array whose first key you want.

Return Value

Return ValueDescription
int|string|nullThe first key of the array, or null if the array is empty.

PHP Examples

Example 1 — Get the First Key

Read the first key of an associative array.

PHP
<?php
$scores = ["ana" => 90, "bob" => 80, "cara" => 70];

echo array_key_first($scores), PHP_EOL;

How It Works

  1. Create an associative array. The first key added is ana.
  2. Call array_key_first(). It looks at the order of the array.
  3. Print the result. The output is the key, not the value 90.
  4. The array stays the same. No internal pointer is moved.

Example 2 — Empty Arrays and Unordered Numeric Keys

See what is returned when the array is empty or the keys are not in numeric order.

PHP
<?php
echo var_export(array_key_first([]), true), PHP_EOL;
echo array_key_first([5 => "a", 2 => "b"]), PHP_EOL;
echo gettype(array_key_first(["x" => 1])), PHP_EOL;

How It Works

  1. Use an empty array. There is no first key, so PHP returns null.
  2. Print null safely. var_export() shows it as NULL.
  3. Use keys 5 then 2. The first key is 5 because order matters, not size.
  4. Check the type. A named key is a string, while a numeric key is an integer.

Example 3 — Process the Next Task in a Queue

Use the first key to read both the id and the value.

PHP
<?php
$queue = ["task-9" => "Send email", "task-3" => "Backup files"];

$id = array_key_first($queue);

echo "Next id: " . $id . PHP_EOL;
echo "Job: " . $queue[$id] . PHP_EOL;

How It Works

  1. Build a queue. Keys are task ids and values are job names.
  2. Get the first key. It is task-9.
  3. Read the job. Use the key to look up the value in the array.
  4. Print both. You now know which task comes next.

Frequently Asked Questions

It returns the first key of an array, or null if the array is empty. It does not change the array.
It was added in PHP 7.3. For older versions you can use reset() and key().
Use $array[array_key_first($array)] after checking the array is not empty. In PHP 8.5 you can also use array_first().
No. It is the key that comes first in the array order. [5 => "a", 2 => "b"] gives 5.
array_key_last() returns the last key of an array.

Next: array_key_last()

array_key_last() returns the last key of an array without changing the array.

array_key_last() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful