How7oHow7o
  • Home
  • Tools
    • All Tools
    • Image Tools
    • Text & String Tools
    • Video Tools
    • Developer Tools
    • PDF Tools
    • Calculators & More
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Get the Last Item from an Array in PHP
Share
Notification Show More
Font ResizerAa
How7oHow7o
Font ResizerAa
  • Tools
  • Prank Screens
  • Learn
  • Blog
Search
  • Home
  • Tools
    • All Tools
    • Image Tools
    • Text & String Tools
    • Video Tools
    • Developer Tools
    • PDF Tools
    • Calculators & More
  • 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.

You Might Also Like

Laravel call controller from another controller — app() and constructor injection patterns
Web Development

How to Call a Controller Method from Another Controller in Laravel

8 Min Read
Laravel MySQL variable in select — @name := assignment chains values across select items
Web Development

How to Set a MySQL Variable in Laravel Query Builder Select

8 Min Read
Laravel unique validation that ignores the current record on update
Web Development

How to Validate a Unique Column on Update in Laravel

4 Min Read
Laravel foreign key constraint linking posts.user_id to users.id in a schema diagram
Web Development

How to Add Foreign Keys in Laravel Migration

6 Min Read
How7oHow7o

Free browser-based tools, prank screens, and step-by-step tech tutorials. No signup, nothing uploaded.

  • Private by design — your files never leave your device
  • Instant — no signup, no install, no waiting
  • Always free — no trials, no watermarks, no paywalls

Tools

  • PDF Editor
  • Image Upscaler
  • JPEG Compressor
  • Password Generator
  • QR Code Generator
  • Video Trimmer
  • See all tools →

Pranks

  • Cracked Screen Prank
  • Fake Blue Screen (BSOD)
  • Fake Disk Format
  • Fake Low Battery
  • Fake Nintendo Switch Update Screen
  • Fake PlayStation Update Screen
  • 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?