Post Details

How to Get Last 10 Elements of Array in PHP
25 Nov

How to Get Last 10 Elements of Array in PHP

Hi Guys,

Today our leading topic is php array get last 10 elements. I would like to share with you that the php array gets 10 last element values. you can see how to get the 10 last elements of an array in PHP. Here you will learn php to get the 10 last keys of the array example. follow the below example for how to get the 10 last key values of the array in PHP.

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

Example: index.php

<?php
$myArray = ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve"];
$lastElems = array_slice($myArray, -10, 10);
var_dump($lastElems);
?>

Output:

array(10) {
   [0]=> string(5) "Three"
   [1]=> string(4) "Four"
   [2]=> string(4) "Five"
   [3]=> string(3) "Six"
   [4]=> string(5) "Seven"
   [5]=> string(5) "Eight"
   [6]=> string(4) "Nine"
   [7]=> string(3) "Ten"
   [8]=> string(6) "Eleven"
   [9]=> string(6) "Twelve"
}

I hope it can help you...

0 Comments

Leave a Comment