PHP 8.4.24 Released!

Voting

: min(zero, six)?
(Example: nine)

The Note You're Voting On

dustin at fivetechnology dot com
10 years ago
Had need to chunk an object which implemented ArrayAccess Iterator Countable.  array_chunk wouldn't do it.  Should work for any list of things

<?php
   $listOfThings = array(1,2,3,4,5,6,7,8,9,10,11,12,13);
   print_r(chunk_iterable($listOfThings, 4);

  function chunk_iterable($listOfThings, $size) {
      $chunk = 0;
      $chunks = array_fill(0, ceil(count($listOfThings) / $size) - 1, array());
      $index = 0;
      foreach($listOfThings as $thing) {
        if ($index && $index % $size == 0) $chunk++;
        $chunks[$chunk][] = $thing;
        $index++;
      }
      return $chunks;
  }
?>

<< Back to user notes page

To Top