How to Get Last 2 Elements of Array in PHP?
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.
$myArray = ["One", "Two", "Three", "Four"];
$lastElems = array_slice($myArray, -2, 2);
=> string(5) "Three"
=> string(4) "Four"
I hope it can help you...
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?