note 94138 added to function.array-slice
| From: | jamie at jamiechong dot ca | Date: | Sun, 18 Oct 2009 23:04:33 +0000 |
| Subject: | note 94138 added to function.array-slice | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-161867@lists.php.net to get a copy of this message | ||
A flexible array_split function:
<?
// split the given array into n number of pieces
function array_split($array, $pieces=2)
{
if ($pieces < 2)
return array($array);
$newCount = ceil(count($array)/$pieces);
$a = array_slice($array, 0, $newCount);
$b = array_split(array_slice($array, $newCount), $pieces-1);
return array_merge(array($a),$b);
}
// Examples:
$a = array(1,2,3,4,5,6,7,8,9,10);
array_split($a, 2); // array(array(1,2,3,4,5), array(6,7,8,9,10))
array_split($a, 3); // array(array(1,2,3,4), array(5,6,7), array(8,9,10))
array_split($a, 4); // array(array(1,2,3), array(4,5,6), array(7,8), array(9,10))
?>
----
Server IP: 216.235.13.26
Probable Submitter: 174.7.4.102
----
Manual Page -- http://www.php.net/manual/en/function.array-slice.php
Edit -- https://master.php.net/note/edit/94138
Del: integrated -- https://master.php.net/note/delete/94138/integrated
Del: useless -- https://master.php.net/note/delete/94138/useless
Del: bad code -- https://master.php.net/note/delete/94138/bad+code
Del: spam -- https://master.php.net/note/delete/94138/spam
Del: non-english -- https://master.php.net/note/delete/94138/non-english
Del: in docs -- https://master.php.net/note/delete/94138/in+docs
Del: other reasons-- https://master.php.net/note/delete/94138
Reject -- https://master.php.net/note/reject/94138
Search -- https://master.php.net/manage/user-notes.php