Follow Sebastiaan de Jonge on Twitter
Sebastiaan de Jonge

Blog

28Dec

PHP: Dividing an array into equal pieces

Posted on December 28, 2010 in PHP by Sebastiaan de Jonge

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.

An easy way to divide an array into equal segments 
  1. function array_divide($array, $segmentCount) {
  2.     $dataCount = count($array);
  3.     if ($dataCount == 0) return false;
  4.     $segmentLimit = ceil($dataCount / $segmentCount);
  5.     $outputArray = array();
  6.  
  7.     while($dataCount > $segmentLimit) {
  8.         $outputArray[] = array_splice($array,0,$segmentLimit);
  9.         $dataCount = count($array);
  10.     }
  11.     if($dataCount > 0) $outputArray[] = $array;
  12.  
  13.     return $outputArray;
  14. }
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() 
  1. function array_divide($array, $segmentCount) {
  2.     $dataCount = count($array);
  3.     if ($dataCount == 0) return false;
  4.     $segmentLimit = ceil($dataCount / $segmentCount);
  5.     $outputArray = array_chunk($array, $segmentLimit);
  6.  
  7.     return $outputArray;
  8. }
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 (4)

  1. Gravatar: Aleksandar MilovacAleksandar Milovacon December 28, 2010
    at 11:29
    Reply to this comment

    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

  2. Gravatar: Victor GerritsenVictor Gerritsenon December 28, 2010
    at 11:35
    Reply to this comment

    array_chunk

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

  3. Gravatar: Sebastiaan de JongeSebastiaan de Jongeon December 28, 2010
    at 11:50
    Reply to this comment

    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.

  4. Gravatar: Victor GerritsenVictor Gerritsenon December 28, 2010
    at 13:31
    Reply to this comment

    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. :-)

Got something to say?

 
Notify me when someone adds another comment to this post
 

Search

Categories

Tags

Archive

Blogroll

Syndicate