Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all items of an array except the last 5 on PHP [duplicate]

Tags:

arrays

php

Possible Duplicate:
Remove elements from array?

how would I remove all array elements but last 5 ones? The array is a log, but the log will become extensive so I just want to see the five recent items (a.k.a. last five elements)

like image 988
pmerino Avatar asked Oct 02 '11 12:10

pmerino


2 Answers

Use array_slice:

$a5 = array_slice($arr, -5);
like image 110
Kerrek SB Avatar answered Oct 07 '22 01:10

Kerrek SB


$new = array_slice($old, -5)
like image 30
Evert Avatar answered Oct 06 '22 23:10

Evert