PHP array_first() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_first() returns the first value of an array, no matter what its key is. If the array is empty, it returns null.

You use array_first() to get the first item of a list or of a filtered result without worrying about the keys. After array_filter() the first key is often not 0, so $array[0] fails, but array_first() still works.

Syntax

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

PHP
$first = array_first($colors);
CallWhat it does
array_first(['red', 'green', 'blue'])Returns 'red', the first value.
array_first([5 => 'x', 2 => 'y'])Returns 'x', because it comes first, even though its key is 5.
array_first([])Returns null for an empty array.

array_first() was added in PHP 8.5. On older versions, use reset($array), which returns false on an empty array, or $array[array_key_first($array)] after you check that the array is not empty. The array is not changed and its internal pointer is not moved.

Library declarations
PHP
array_first(array $array): mixed

Parameters

ParameterDescription
arrayRequired. The array to read from.

Return Value

Return ValueDescription
mixedThe first value of the array, or null when the array is empty.

PHP Examples

Example 1 — Reading the First Value

Get the first item of a simple list.

PHP
<?php
$colors = ['red', 'green', 'blue'];

echo array_first($colors), PHP_EOL;

How It Works

  1. $colors is a list with three values.
  2. array_first() looks at the item that comes first in the array.
  3. That item is 'red'.
  4. echo prints the returned value.

Example 2 — Empty Arrays, Odd Keys and null

The first value is chosen by order, not by key. Empty arrays and a first value of null both give null.

PHP
<?php
var_dump(array_first([]));
echo array_first([5 => 'x', 2 => 'y']), PHP_EOL;
var_dump(array_first([null, 1]));

How It Works

  1. An empty array has no first value, so the result is null.
  2. In the second array the item with key 5 is written first, so 'x' is returned.
  3. In the third array the first value is itself null.
  4. The result is null again, so null alone does not prove that the array is empty.

Example 3 — First Value After Filtering

array_filter() keeps the old keys, so $array[0] may not exist. array_first() does not care about keys.

PHP
<?php
$filtered = array_filter([0, 0, 7, 8]);

echo implode(',', array_keys($filtered)), PHP_EOL;
echo array_first($filtered), PHP_EOL;

How It Works

  1. array_filter() removes the two zeros because they are falsy.
  2. The values 7 and 8 keep their old keys, 2 and 3.
  3. $filtered[0] would not exist here, because there is no key 0.
  4. array_first() returns 7, the first remaining value, whatever its key is.

Frequently Asked Questions

It returns the first value of an array. If the array is empty, it returns null. It does not depend on the array keys.
array_first() was added in PHP 8.5. On older versions use reset(), or read $array[array_key_first($array)] after checking that the array is not empty.
$array[0] reads the item with the key 0, which may not exist after filtering or for associative arrays. array_first() returns the first item in order, whatever its key is.
No. Unlike reset(), array_first() does not change the internal pointer of the array and it does not modify the array.
Use array_key_first(). It returns the first key of the array, or null when the array is empty.

Next: array_flip()

array_flip() swaps the keys and the values of an array. The old values become the new keys.

array_flip() 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