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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Get Last Element of Array in PHP?
- How to Use ChatGPT API in PHP?
- How to Get First Element of Array in PHP?
- How to Remove Last Character from String in PHP?
- PHP Crop Image Before Upload using Croppie JS Example
- How to Remove Duplicate Values from Array in PHP?
- How to Get Folder Path from File Path in PHP?
- How to Get Filename from Path in PHP?
- How to Convert Array Values to Lowercase in PHP?
- How to Add Prefix in Each Key of PHP Array?
- How to Get Minimum Key Value of Array in PHP?
- How to Count Number of Files in a Directory in PHP?
- How to Find Day Name from Specific Date in PHP?
- How to Remove Null Values from Array in PHP?