Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Divide an array into a desired number of split lists
<?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