Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to divide array into small arrays?

Tags:

php

I have an array with 3200 values. A user chooses a number (let's say 50). I want to divide the big array into smaller arrays, each of them containing 50 values (the last one containing the remainder).

How would you do that?

like image 981
Richard Knop Avatar asked Dec 03 '22 09:12

Richard Knop


2 Answers

array_chunk

e.g.

 $arrays = array_chunk($my_big_array, 50);
like image 120
Jonathan Fingland Avatar answered Feb 02 '23 10:02

Jonathan Fingland


array array_chunk ( array $input , int $size [, bool $preserve_keys = false ] )

Check this: http://php.net/manual/en/function.array-chunk.php

like image 42
UltraInstinct Avatar answered Feb 02 '23 08:02

UltraInstinct