Blog
28DecPHP: Dividing an array into equal pieces
Posted on December 28, 2010 in PHP bySomething I needed to do a while ago, and I thought it wouldn't be a bad idea to share it. It's a quick way to divide an array into equal segments. Just take a look at the function code.
An easy way to divide an array into equal segments 
- function array_divide($array, $segmentCount) {
- $dataCount = count($array);
- if ($dataCount == 0) return false;
- $segmentLimit = ceil($dataCount / $segmentCount);
- $outputArray = array();
- while($dataCount > $segmentLimit) {
- $outputArray[] = array_splice($array,0,$segmentLimit);
- $dataCount = count($array);
- }
- if($dataCount > 0) $outputArray[] = $array;
- return $outputArray;
- }
function array_divide($array, $segmentCount) {
$dataCount = count($array);
if ($dataCount == 0) return false;
$segmentLimit = ceil($dataCount / $segmentCount);
$outputArray = array();
while($dataCount > $segmentLimit) {
$outputArray[] = array_splice($array,0,$segmentLimit);
$dataCount = count($array);
}
if($dataCount > 0) $outputArray[] = $array;
return $outputArray;
}
Understanding how the function works shouldn't be too hard. We simply count the amount of items inside the input array, divide it by the amount of segments we would like to have and ceil() that value. Next, we simply start slicing off pieces from the input array by using array_splice() until the data inside the array is less than our segment limit. Finally we will add the remaining data of the input array (if any is left) as the final segment.
Performance
I've done some performance testing with this snippet, to see if it was any good. On my test server I managed to run the function on an array with about 123.000 items, dividing them into 123 equal segments, in 1.6~1.8 seconds. Of course normal, smaller arrays will be a lot faster.
Update: 2010-12-28 13:00
Victor Gerritsen has pointed out the use of array_chunk() instead of array_slice(). And indeed this actually seems like a much more logical option. Not only does it spare some lines of code and array keys, it's also pretty much faster than using array_slice(). I benchmarked it at around 0.3~0.4 seconds. Significantly faster. Thanks Victor
The snippet now looks like this.
A quicker way, using array_chunk() instead of array_slice() 
- function array_divide($array, $segmentCount) {
- $dataCount = count($array);
- if ($dataCount == 0) return false;
- $segmentLimit = ceil($dataCount / $segmentCount);
- $outputArray = array_chunk($array, $segmentLimit);
- return $outputArray;
- }
function array_divide($array, $segmentCount) {
$dataCount = count($array);
if ($dataCount == 0) return false;
$segmentLimit = ceil($dataCount / $segmentCount);
$outputArray = array_chunk($array, $segmentLimit);
return $outputArray;
}



at 11:29
very nice :)
Nice one, Sebastian.
I found your blog when googling for some TS TYPO3 introduction articles, but there are other great articles too. Please, post something about your experiences with arduino :) Maybe TYPO3 and arduino can work together, too :)
Thanks for sharing your ideas.
Best regards,
Aleksandar
at 11:35
array_chunk
You could also use php's own array_chunk function, for the same goal + the option to preserve keys.
at 11:50
Thanks guys
Thanks Aleksandar, I hope I will get the oppurtinity to do so next year
@Victor: You are absolutely right. I've updated the post and added array_chunk instead. Unfortunately for most programming jobs we don't get to use all the fancy array functions that PHP offers. Not only are the keys preserved as well, it's also a lot faster.
at 13:31
No problem
The only reason i know this function exists is because i wrote a similair function, only to find out it already existed. Hehe.
But no harm done of course, it's always a good exercise. :-)