How to Get Last 3 Elements of Array in 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...
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 Random Value from an Array in PHP?
- 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?
- PHP Dropzone Display Existing Files from Server Example
- PHP MongoDB CRUD Operation Example
- PHP MySQL Login with Google Account Example
- PHP MySQL DataTables Server-side Processing Example
- How to Check If String is URL or Not in PHP?
- How to Convert Array Key to Lowercase in PHP?
- How to access PHP variables in JQuery?
- How to Find Day Name from Specific Date in PHP?
- How to Remove Null Values from Array in PHP?