PHP and Laravel give you at least five ways to get the last element of an array. Some are one-liners. Some have side effects. The right choice depends on whether you’re in plain PHP, working with Laravel helpers, or dealing with an Eloquent collection. This guide covers all five — with notes on what each one actually does to your array.
Arr::last($array)— Laravel’s safest option, no side effects, supports a filter callbacklast($array)— Laravel global helper, same result, shorter syntaxend($array)— plain PHP, fast, but moves the internal array pointer (side effect)array_slice($array, -1)[0]— plain PHP, no side effects, slightly more verbose$collection->last()— Eloquent / Collection method, works on objects too
How do you get the last element with Laravel’s Arr::last()?
Arr::last() is the most capable option in Laravel. Import the class and call it statically:
use Illuminate\Support\Arr;
$languages = ['PHP', 'Python', 'Ruby', 'Go', 'Rust'];
$last = Arr::last($languages); // 'Rust' It takes three parameters: the source array, an optional callback for conditional filtering, and an optional default value if nothing matches.
The callback form is useful when you don’t just want the last item — you want the last item that passes a condition:
$numbers = [12, 24, -8, 55, 100, 1331, 766, 65];
$lastLargeNumber = Arr::last($numbers, fn($value) => $value >= 100);
// Result: 766 — the last item that's 100 or above If no item passes the test, Arr::last() returns null by default. Pass a third argument to return something else:
$lastOver2000 = Arr::last($numbers, fn($value) => $value >= 2000, 0);
// Result: 0 — fallback because no value meets the condition Arr::last() never modifies the source array. No side effects.
How does the global last() helper work?
last() is a global Laravel helper. It’s a shortcut for Arr::last() without the import:
$carBrands = ['Maserati', 'Tesla', 'Tata', 'Aston Martin', 'Audi', 'Alfa Romeo'];
$final = last($carBrands); // 'Alfa Romeo' It doesn’t accept a callback. If you need filtering, use Arr::last(). If you just need the last value, last() is the shortest form.
The scenario: You’re debugging a pipeline that processes steps sequentially. You want to log whatever the final step was.
last($steps)in the log line is three words, readable, and doesn’t require an import. That’s the whole reason it exists.
What does PHP’s end() do — and why is it a problem?
end() is built into PHP. It returns the last element and moves the array’s internal pointer to the end:
$fruits = ['Orange', 'Apple', 'Grapes', 'Kiwi', 'Mango'];
$last = end($fruits); // 'Mango' That sounds fine until you loop over the same array afterward. PHP’s next(), prev(), current(), and reset() all depend on that internal pointer. Moving it with end() can break a foreach-style iteration that’s happening elsewhere on the same array reference.
end() modifies the internal array pointer of the variable you pass to it. If the same array is used in a loop or passed by reference anywhere else in scope, the position change can cause unexpected behavior. Prefer Arr::last() or array_slice() when side effects are a concern.
For one-off scripts or cases where you control the full array lifecycle, end() is fine. In shared code or class methods, it’s a trap.
How does array_slice() give you the last element without side effects?
array_slice($array, -1) returns a new one-element array containing the last item. Access index [0] to get the value:
$scores = [88, 72, 95, 61, 84];
$last = array_slice($scores, -1)[0]; // 84 No pointer movement. No imports. Works in any PHP version. The downside: it’s more verbose than last() and creates an intermediate array in memory. For single elements on a small array, that’s irrelevant. For tight loops on large arrays, prefer end() or Arr::last().
How do you get the last item from an Eloquent Collection?
Eloquent query results return a Collection object. It has its own last() method:
$users = User::orderBy('created_at')->get();
$newestUser = $users->last(); This returns the last Model in the collection — the full object, not just a scalar value. You can also pass a callback to get the last item matching a condition:
$lastAdmin = User::all()->last(fn($user) => $user->role === 'admin'); If no item matches, it returns null.
The scenario: You’re building a “last activity” widget on an admin dashboard. You’ve already loaded a collection of events for the current session. Rather than running another query,
$events->last()pulls the most recent one from the in-memory collection. One method call, no extra DB hit.
See Create a Laravel Collection from an Array if you need to wrap a plain array as a Collection first.
Which method should you use?
| Situation | Best choice |
|---|---|
| Laravel project, no filter needed | last($array) |
| Laravel project, need conditional filtering | Arr::last($array, $callback) |
| Plain PHP, no Laravel available | array_slice($array, -1)[0] |
| Plain PHP, you own the full array scope | end($array) |
| Eloquent Collection | $collection->last() |
Summary
Arr::last()is the most flexible Laravel option — it supports callbacks and defaults, with no side effects.last()is the global helper shortcut. Good for the simple case.end()moves the internal array pointer — use it carefully in shared code.array_slice($array, -1)[0]is the clean plain-PHP option with no side effects.$collection->last()works on Eloquent results and supports an optional callback.
FAQ
Does last() work on associative arrays?
Yes. Both last() and Arr::last() return the last value regardless of whether the keys are integers or strings.
What does Arr::last() return if the array is empty?
It returns null by default. Pass a third argument to specify a different fallback value.
Can I use Arr::last() without importing the class?
No. You need use Illuminate\Support\Arr; at the top of the file. Alternatively, use the global last() helper which requires no import.
Is there a way to get the last key as well as the last value?
Use array_key_last($array) (PHP 7.3+) for the key, then $array[array_key_last($array)] for the value. Arr::last() only returns the value.
Does end() work on empty arrays?
It returns false on an empty array. Check with empty($array) before calling end() if the array might be empty.
What to Read Next
- Create a Laravel Collection from an Array — wrap your array in a Collection to get
->last()and much more. - Remove Elements from a Laravel Collection — trim the collection before calling
last()to get the last matching item. - Get an Array of IDs from an Eloquent Collection — a related pattern for extracting specific values from a collection.
Related Articles
Deepen your understanding with these curated continuations.
Laravel Arr::flatten(): Flatten Nested Arrays
How to flatten multi-dimensional PHP arrays with Laravel's Arr::flatten(). Covers the depth parameter, key discard behavior, and comparison with plain PHP.
Unsigned Columns in Laravel Migrations
How to declare unsigned integer columns in Laravel migrations using unsignedBigInteger(), foreignId(), and the unsigned() modifier. Updated for Laravel 12.
Nested Eager Loading in Laravel Eloquent Explained
Reduce database queries in Laravel using nested eager loading. Learn the syntax, conditional loading, and how to optimize deeply related Eloquent models.