How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Get the Last Item from an Array in PHP
Share
How7oHow7o
Font ResizerAa
  • OS
Search
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Free Laravel, PHP, WordPress & Server Tutorials > Web Development > How to Get the Last Item from an Array in PHP
Web Development

How to Get the Last Item from an Array in PHP

how7o
By how7o
Last updated: May 22, 2026
5 Min Read
Get the last item from a PHP array
SHARE

To get the last item from an array in PHP, use end($arr) for a quick read, or $arr[array_key_last($arr)] for a side-effect-free version (PHP 7.3+). For sequential numeric-keyed arrays, $arr[count($arr) - 1] also works.

Contents
  • The one-liner
  • Without moving the internal pointer
  • Sequential numeric arrays
  • Get and remove in one call
  • Empty-array safety
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-05-17 on PHP 8.3. Originally published 2024-02-27, rewritten and updated 2026-05-17.

The one-liner

$arr  = ['apple', 'banana', 'cherry'];
$last = end($arr);
// 'cherry'

end() moves the array’s internal pointer to the last element and returns its value. Works on any array shape (sequential, associative, gappy keys).

PHP last array item — end(), array_key_last(), array_slice, array_pop compared

Without moving the internal pointer

// PHP 7.3+
$arr   = ['a' => 1, 'b' => 2, 'c' => 3];
$last  = $arr[array_key_last($arr)];
// 3

// Or, equivalent and cheap
$last  = array_slice($arr, -1)[0] ?? null;
// 3

array_key_last() returns the last key without touching the internal pointer, and bracket-lookup gives you the value. Use this when the array might also be iterated elsewhere — end() would leave that iteration in a confusing state.

Sequential numeric arrays

$arr  = [10, 20, 30, 40];
$last = $arr[count($arr) - 1];
// 40

Only works when the keys are 0, 1, 2, …, count−1. Breaks on associative arrays, and breaks after unset creates a gap. Reach for end() or array_key_last() instead — same speed, no shape assumption.

Get and remove in one call

$arr  = ['apple', 'banana', 'cherry'];
$last = array_pop($arr);
// $last = 'cherry'
// $arr  = ['apple', 'banana']

array_pop() mutates the array — use this when you want the last item and intend to remove it (stack-style processing). It’s not the right tool when you only want to read.

Empty-array safety

$arr  = [];

end($arr);                                   // false (no warning)
$arr[array_key_last($arr)] ?? null;          // null (array_key_last returns null)
array_slice($arr, -1)[0] ?? null;            // null
array_pop($arr);                             // null (and array stays [])

All four handle empty arrays without throwing. The return values differ — end() gives false (which looks like a found value of 0 or empty string until you compare with ===), while the others return null. Pick the one whose empty-case return type matches your downstream code.

Frequently asked questions

Does end() modify the original array?

It moves the array’s internal pointer to the last element — it doesn’t remove or change any data, but it does affect the array’s iteration state. If you immediately follow with current() or reset() elsewhere, the pointer position can surprise you. For pure read access without side effects, array_key_last() + bracket lookup is cleaner.

Why does $arr[count($arr) - 1] sometimes fail?

It assumes the keys are sequential integers starting from 0. On associative arrays or arrays where keys have been deleted (creating gaps), count($arr) - 1 doesn’t correspond to a real key. $arr['last_inserted_at_index_5'] isn’t reachable as $arr[3] just because there are four items. end() or array_key_last() work on any array, sequential or not.

What about array_slice($arr, -1)[0]?

Works correctly on every array shape and doesn’t move the internal pointer — but it creates an extra single-element array, which is slightly wasteful for a hot path. Fine for clarity; not the right call when you’re iterating thousands of times. Prefer end() or array_key_last() when performance matters.

How do I get the last item and remove it from the array?

array_pop($arr) returns and removes the last element in one call. Mutates the array. Useful when you want the last item exactly because you intend to consume it (e.g. stack-like processing).

Related guides

  • How to Combine Two Arrays Without Duplicates in PHP
  • How to Delete an Element from an Array in PHP
  • How to Get the First Character of a String in PHP

References

PHP end(): php.net/manual/en/function.end.php. PHP array_key_last(): php.net/manual/en/function.array-key-last.php. PHP array_pop(): php.net/manual/en/function.array-pop.php.

TAGGED:arraysphp

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
[mc4wp_form]
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article Get the first character of a PHP string with mb_substr for UTF-8 How to Get the First Character of a String in PHP
Next Article Get the selected radio button value with jQuery How to Get the Selected Radio Button Value in jQuery
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow
Most Popular
Run Laravel queue workers with Supervisor
How to Run Laravel Queue Workers in Production with Supervisor
May 23, 2026
Nginx as a reverse proxy for a Node.js app on Ubuntu
How to Set Up Nginx as a Reverse Proxy for Node.js on Ubuntu
May 23, 2026
Install and configure Redis on Ubuntu for Laravel and WordPress
How to Install and Configure Redis on Ubuntu (for Laravel & WordPress)
May 23, 2026
Harden a fresh Ubuntu VPS with UFW, Fail2Ban, and SSH key auth
How to Harden a Fresh Ubuntu VPS: UFW + Fail2Ban + SSH Key Auth
May 23, 2026
Set up Let's Encrypt SSL with Certbot on Ubuntu
How to Set Up Let’s Encrypt SSL with Certbot on Ubuntu (Apache & Nginx)
May 23, 2026

You Might Also Like

Laravel Eloquent delete record — trash-bin icon next to User::destroy code snippet
Web Development

How to Delete a Record with Laravel Eloquent (4 Methods)

6 Min Read
WordPress get current category ID — three methods by page context
Web Development

How to Get the Current Category ID in WordPress

7 Min Read
Laravel Blade @foreach vs @forelse comparison
Web Development

Laravel Blade: Difference Between @foreach and @forelse

4 Min Read
jQuery add required attribute to input fields — .prop() method
Web Development

How to Add the Required Attribute to Input Fields with jQuery

5 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Tools

  • Age Calculator
  • Word Counter
  • Image Upscaler
  • Password Generator
  • QR Code Generator
  • See all tools→

Pranks

  • Fake Blue Screen Prank
  • Hacker Typer
  • Fake iMessage Generator
  • Windows XP Crash Prank
  • Windows 11 Update Prank
  • See all prank screens →

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service
  • Sitemap
© 2024–2026 How7o. All rights reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?