By Sebastiaan de Jonge, published on Tuesday, December 28, 2010 at 10:16

Something 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.

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 Blinkert!

The snippet now looks like this.

function array_divide($array, $segmentCount) {
    $dataCount = count($array);
    if ($dataCount == 0) return false;
    $segmentLimit = ceil($dataCount / $segmentCount);
    $outputArray = array_chunk($array, $segmentLimit);
    return $outputArray;
}

Comments

Aleksandar Milovac
Aleksandar Milovac - Tuesday, December 28, 2010 at 11:29

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

Victor Gerritsen
Victor Gerritsen - Tuesday, December 28, 2010 at 11:35

You could also use php's own array_chunk function, for the same goal + the option to preserve keys.

Sebastiaan de Jonge
Sebastiaan de Jonge - Tuesday, December 28, 2010 at 11:50

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.

Victor Gerritsen
Victor Gerritsen - Tuesday, December 28, 2010 at 13:31

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. 🙂

online writing
online writing - Friday, August 5, 2011 at 17:00

i get confuse with this help guys for it!

Schiffsratte
Schiffsratte - Monday, July 29, 2013 at 18:18

If you use floor instead of ceil, the parts returned will be more equal.For example an array with 12 elements will return 4-4-4-2 if you round with ceiland the same with floor will return 3-3-3-2 which can be useful for lists