array_last

Returns the last value of an array without moving the internal pointer, or null if the array is empty.

Signature
array_last(array $array): mixed
  • Added in PHP 8.5

Parameters

ParameterTypeDescription
$arrayarrayAn array.

Return value

mixedThe last value of array if the array is not empty; null otherwise.

Errors

Throws a TypeError if the argument is not an array.

array_last() returns the value at the array's final position in insertion order. It is equivalent to $array[array_key_last($array)] with the empty case handled, returning null when there are no elements.

The two older approaches both had drawbacks. end($array) requires a variable, because it takes its argument by reference, and it leaves the internal pointer parked at the end — a subtle bug when the same array is iterated with current() or next() afterwards. $array[count($array) - 1] only works on a well-formed list; any unset(), array_filter() or string key breaks it.

It does not remove anything

array_last() is a pure read. If you meant to take the element off the end, use array_pop(), which shortens the array and also resets the pointer.

Ordering

"Last" means last inserted, not largest key. Sort the array first if you want the maximum, or use max() on the values.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Read the most recent entry from an append-only log or history array.
  • Take the final segment of a path after explode() without counting elements.
  • Inspect the last item of a paginated result set to build a cursor for the next page.

Frequently asked questions

What is the difference between array_last and end?

end() moves the internal array pointer to the last element and requires a variable argument, while array_last() only reads the value and accepts any array expression. Use end() only when you actually want the pointer moved.

Does array_last remove the element?

No, it is a read-only lookup. Use array_pop() if you want to return the last element and remove it from the array.

Is array_last the same as $array[count($array) - 1]?

Only for a proper list with sequential keys starting at 0. After array_filter(), unset(), or on any string-keyed array that expression breaks, while array_last() still returns the final value.

More examples

Last segment of a split string

Prints "app.php", then "var", then "php". Because array_last() takes an expression rather than a reference, the last line nests two calls — something end() cannot do without a temporary variable.

Example · php
<?php
$path = 'var/www/site/config/app.php';
$parts = explode('/', $path);

var_dump(array_last($parts));
var_dump(array_first($parts));

$ext = array_last(explode('.', array_last($parts)));
var_dump($ext);

array_last versus end and array_pop

array_last() returns "c" while current() still reports "a". end() returns the same value but leaves the pointer on "c", and array_pop() returns "c" and shrinks the array to two elements.

Example · php
<?php
$queue = ['a', 'b', 'c'];

var_dump(array_last($queue));
var_dump(current($queue));   // pointer untouched

end($queue);
var_dump(current($queue));   // pointer moved to the end

var_dump(array_pop($queue));
print_r($queue);             // element removed

Discussion

  • Be the first to comment on this lesson.