• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / PHP / Get Last Element of Array in php

Get Last Element of Array in php

March 14, 2022 Leave a Comment

To get the last element of an array in php, the easiest way is with the end() function.

$array = array(1, 2, 3, 4, 5);

echo end($array);

// Output:
5

Another way to get the last element of an array is with the array_slice() function.

$array = array(1, 2, 3, 4, 5);

echo array_slice($array, -1)[0];

// Output:
5

A third way to get the last element of an array is with the array_pop() function.

$array = array(1, 2, 3, 4, 5);

echo array_pop($array);

// Output:
5

When working with arrays in php, it can be valuable to be able to easily filter and get only specific values from your array.

One such situation is when you want to get the last element from a php array.

There are a number of ways to get the last element from a php array, and the easiest way is with the end() function.

Below is an example of how to print the last element of a php array with end().

$array = array(1, 2, 3, 4, 5);

echo end($array);

// Output:
5

If you are looking to get the first element of a php array, the easiest way is by accessing the first element directly.

$array = array(1, 2, 3, 4, 5);

echo $array[0];

// Output:
1

Getting the Last Element of an Array in php with array_slice()

Another way we get the last element of an array in php with the php array_slice() function.

You can pass php array_slice() function a starting position and ending position, and get the elements in the array between those two positions.

To get the last element of an array in php, pass ‘-1’ to array_slice(). You don’t need to pass an ending position since by default the ending position is the end of the array.

Below is a simple example of how to get the last element of an array with array_slice() using php.

$array = array(1, 2, 3, 4, 5);

echo array_slice($array, -1)[0];

// Output:
5

Getting the Last Element of an Array in php with array_pop()

Another way to get the last element of an array is with the php array_pop() function.

The array_pop() function ‘pops’ and removes the last element from an array.

We can use array_pop() to return the last element of a php array.

Below is how to get the last element of a php array with array_pop().

$array = array(1, 2, 3, 4, 5);

echo array_pop($array);

// Output:
5

Getting the Last N Elements of an Array in php with array_slice()

If you are looking to get more than just the last element, we can get the last n elements of a php array with the array_slice() function.

You can pass php array_slice() function a starting position and ending position, and get the elements in the array between those two positions.

To get the last 3 elements of an array in php, pass ‘-3’ to array_slice(). We don’t need to pass an ending position since we will be going to the end of the array.

Below is a simple example of how to get the last three elements of an array using php.

$array = array(1, 2, 3, 4, 5);

echo var_dump(array_slice($array,-3));

// Output:
array(3) {
  [0]=>
  int(3)
  [1]=>
  int(4)
  [2]=>
  int(5)
}

Hopefully this article has been useful for you to learn how to get the last element of an array when using php.

Other Articles You'll Also Like:

  • 1.  Capitalize First Letter of String with php ucfirst() Function
  • 2.  Using strtolower() in php to Convert String to Lowercase
  • 3.  Truncate Strings in php Using substr()
  • 4.  Remove Specific Character from String Variable in php
  • 5.  php ceil – Round Up and Find the Ceiling of a Number in php
  • 6.  php in_array – Check If Value is in php Array
  • 7.  php e – Using M_E and exp() Function to Get Euler’s Constant e
  • 8.  How to Create an Empty Array in php
  • 9.  preg_replace php – Use Regex to Search and Replace
  • 10.  How to Parse URLs with php parse_url() Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy