How to Get Last 3 Elements of Array in PHP?

By Hardik Savani November 5, 2023 Category : PHP

Hey Friends,

Here, I will show you how to work php array get last 3 elements. This tutorial will give you a simple example of php array get 3 last element value. I would like to show you how to get 3 last element of array in php. This tutorial will give you a simple example of php get 3 last key of array example.

we will use array_slice() function to get last 3 elements of array in php. so, let's see the simple code of how to get last 3 values in php array.

Example:

index.php

<?php

$myArray = ["One", "Two", "Three", "Four", "Five", "Six"];

$lastElems = array_slice($myArray, -3, 3);

var_dump($lastElems);

?>

Output:

array(3) {

[0]=> string(4) "Four"

[1]=> string(4) "Five"

[2]=> string(3) "Six"

}

I hope it can help you...

Tags :
Shares