Created
March 29, 2016 11:03
-
-
Save blacksmoke26/40c7f8a2a529702e8f1a to your computer and use it in GitHub Desktop.
Divide an array into a desired number of split lists
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Divide an array into a desired number of split lists | |
* @author Azspot | |
* @link http://www.php.net/manual/en/function.array-chunk.php#75022 | |
* @param array $list The Array | |
* @param integer $size Partition Size | |
* @return array Output array | |
*/ | |
function columnsPartition ( array $list, $size ) | |
{ | |
$listlen = count( $list ); | |
if ( !$listlen || $size < 1) | |
{ | |
return array (); | |
} | |
$partlen = floor( $listlen / $size ); | |
$partrem = $listlen % $size; | |
$partition = []; | |
$mark = 0; | |
for ($px = 0; $px < $size; $px++) | |
{ | |
$incr = ($px < $partrem) | |
? $partlen + 1 | |
: $partlen; | |
$partition[$px] = array_slice( $list, $mark, $incr ); | |
$mark += $incr; | |
} | |
return $partition; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment