Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shorten Array in PHP?

Tags:

php

I want to shorten an array so it only contains 30 elements. If for example I have an array of 100 elements is it possible to take it and chop of (as to speak) 70 of those elements?

like image 416
Jamie Redmond Avatar asked Aug 19 '10 10:08

Jamie Redmond


2 Answers

Use array_slice to extract the range of elements you need.

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

$short_array will have first 30 elements of $my_big_array

like image 75
rubayeet Avatar answered Oct 14 '22 23:10

rubayeet


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

Usage:

$shortarray = array_slice($longarray, 0, 30);
like image 28
tamasd Avatar answered Oct 14 '22 23:10

tamasd