How to Get Last 5 Elements in PHP Array?

Nov 22, 2022

.

Admin

How to Get Last 5 Elements in PHP Array?

Hi Dev,

This tutorial is focused on how to get the last 5 elements in a PHP array. you'll learn how to get the last 5 elements in the array. We will use how to find the last 5 elements in a PHP array. if you have a question about how to find the last 5 elements in the array then I will give a simple example with a solution.

Now, let's see the article on how to get the last 5 elements in a PHP array. it's a simple example of how to get the last 5 elements in the array. you can understand the concept of how to find the last 5 elements in a PHP array. if you have a question about how to find the last 5 elements in the array then I will give a simple example with a solution.

Example:


index.php

<?php

$colors = array("red", "green", "blue", "purple", "pink", "yellow", "black", "white", "orange", "golden", "silver", "brown", "gray", "maroon", "fuchsia", "lime", "olive", "navy", "teal", "aqua", "violet");

print_r(array_slice($colors,-5,5));

?>

Output:

Array ( [0] => olive [1] => navy [2] => teal [3] => aqua [4] => violet )

I hope it could help you...

#PHP