How to Get Last 2 Elements of Array in PHP?
Hi Guys,
In this example, you will learn php array get last 2 elements. This article goes in detailed on php array get 2 last element value. I would like to show you how to get 2 last element of array in php. This example will help you php get 2 last key of array example.
we will use array_slice() function to get last 2 elements of array in php. so, let's see the simple code of how to get last 2 values in php array.
Example:
index.php
<?php
$myArray = ["One", "Two", "Three", "Four"];
$lastElems = array_slice($myArray, -2, 2);
var_dump($lastElems);
?>
Output:
array(2) {
[0]=> string(5) "Three"
[1]=> string(4) "Four"
}
I hope it can help you...