When working with PHP arrays, you sometimes need to retrieve the last value without knowing its key or numeric index.
The array_last() function makes this simple. It works with indexed and associative arrays, including arrays with non-sequential keys.
PHP version requirement: array_last() was introduced in PHP 8.5.
What Is array_last() in PHP?
The array_last() function returns the value of the last element in an array.
Its syntax is:
array_last(array $array): mixed
The function accepts one parameter, $array, and returns:
- The last element's value when the array is not empty.
nullwhen the array is empty.
The return type is mixed because array values can have different data types, including strings, integers, arrays, and objects.
The function does not modify the original array or change its internal pointer.
Example 1: Getting the Last Value
Let's retrieve the last programming language from an indexed array.
<?php
$languages = [
"PHP",
"Python",
"JavaScript"
];
$last = array_last($languages);
echo $last;
Output:
JavaScript
The function returns "JavaScript" because it is the last element in the array.
Unlike array_find(), array_last() doesn't accept a callback or search for a matching condition. It simply returns the final value.
Example 2: Working with Associative Arrays
The function also works when array keys are strings or non-sequential integers.
<?php
$products = [
100 => "Keyboard",
50 => "Mouse",
10 => "Monitor"
];
echo array_last($products);
Output:
Monitor
Even though the last element has the smallest numeric key, PHP returns its value.
Important: PHP arrays maintain element order. The last element is not necessarily the one with the largest numeric key or the last alphabetically sorted key.
If an array is sorted or reordered, the value returned by array_last() may change.
What Happens with Empty Arrays?
When the input array is empty, array_last() returns null.
<?php
$items = [];
var_dump(array_last($items));
Output:
NULL
However, null can also be a valid value stored in an array:
<?php
$items = ["Ready", null];
var_dump(array_last($items));
This also returns NULL.
If your application needs to distinguish between an empty array and a last element containing null, check the array first:
if ($items !== []) {
$last = array_last($items);
// The array contains at least one element.
}
Practical Example: Displaying the Latest Activity
Imagine you're developing an application that maintains an activity history. Each new activity is appended to an array, so the final element represents the most recently recorded event.
We can use array_last() to display that activity.
Create a file named latest-activity.php:
<?php
declare(strict_types=1);
// Activities recorded in chronological order.
$activities = [
[
"time" => "09:00",
"action" => "Account created"
],
[
"time" => "09:30",
"action" => "Profile updated"
],
[
"time" => "10:15",
"action" => "Password changed"
]
];
// Retrieve the last activity.
$latestActivity = array_last($activities);
if ($latestActivity !== null) {
$message = $latestActivity["time"]
. " - "
. $latestActivity["action"];
echo "<h2>Latest Activity</h2>";
echo "<p>" . htmlspecialchars(
$message,
ENT_QUOTES,
"UTF-8"
) . "</p>";
} else {
echo "<p>No activities recorded.</p>";
}
Browser output:
Latest Activity
10:15 - Password changed
The function returns the final activity record, which is itself an associative array.
We then access its time and action fields.
This approach works when records are already arranged in chronological order. If records arrive out of order, compare their timestamps instead of assuming the last array element is the newest.
array_last() vs Related Functions
| Function | Behavior |
|---|---|
array_last() |
Returns the last value without modifying the array. PHP 8.5+. |
array_key_last() |
Returns the last key, not the value. PHP 7.3+. |
array_first() |
Returns the first value. PHP 8.5+. |
end() |
Moves the internal array pointer to the last element and returns its value. |
array_pop() |
Removes and returns the last value, modifying the array. |
Use array_last() when you only need to read the final value. Use array_pop() when you also intend to remove that element.
Using PHP Versions Earlier Than 8.5
If your application uses PHP 7.3 through PHP 8.4, you can retrieve the last value using array_key_last().
<?php
$colors = [
"primary" => "Blue",
"secondary" => "Green",
"accent" => "Orange"
];
$lastKey = array_key_last($colors);
$lastValue = $lastKey === null
? null
: $colors[$lastKey];
var_dump($lastValue);
Output:
string(6) "Orange"
This retrieves the final key and then accesses its value. Like array_last(), it handles empty arrays without modifying the internal array pointer.
Key Takeaways
array_last()was introduced in PHP 8.5.- It retrieves the final value from indexed or associative arrays.
- The result depends on element order, not numeric or alphabetical key order.
- An empty array returns
null. - The function does not modify the array or its internal pointer.
- Use
array_key_last()when you need the final key instead. - For older PHP versions, combine
array_key_last()with an array lookup.
The array_last() function provides a clear way to retrieve the final value of a PHP array. It is especially useful for accessing the most recent item in an ordered collection without modifying the original data.
For additional details, see the official PHP array_last() documentation.