Shrink an array in PHP?

I want to shorten the array, so it only contains 30 elements. If, for example, I have an array of 100 elements, can I take it and cut (as they say) 70 of these elements?

+7
php
source share
2 answers

Use array_slice to extract the range of elements needed.

 $short_array = array_slice($my_big_array, 0, 30) 

$short_array will have the first 30 elements of $my_big_array

+30
source share

http://php.net/manual/en/function.array-slice.php

Using:

 $shortarray = array_slice($longarray, 0, 30); 
+10
source share

All Articles