note 118972 added to function.array-chunk
| From: | dustin at fivetechnology dot com | Date: | Thu, 10 Mar 2016 18:22:02 +0000 |
| Subject: | note 118972 added to function.array-chunk | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-205129@lists.php.net to get a copy of this message | ||
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;
}
?>
----
Server IP: 72.52.91.14
Probable Submitter: 2601:280:8102:ab00:dbe:f118:3b2e:10a4
----
Manual Page -- http://php.net/manual/en/function.array-chunk.php
Edit -- https://master.php.net/note/edit/118972
Del: integrated -- https://master.php.net/note/delete/118972/integrated
Del: useless -- https://master.php.net/note/delete/118972/useless
Del: bad code -- https://master.php.net/note/delete/118972/bad+code
Del: spam -- https://master.php.net/note/delete/118972/spam
Del: non-english -- https://master.php.net/note/delete/118972/non-english
Del: in docs -- https://master.php.net/note/delete/118972/in+docs
Del: other reasons-- https://master.php.net/note/delete/118972
Reject -- https://master.php.net/note/reject/118972
Search -- https://master.php.net/manage/user-notes.php