[Solved] PHP Function to devide an array of N elements in N sub array


Try this function from php.net.

function partition($list, $p) {
    $listlen = count($list);
    $partlen = floor($listlen / $p);
    $partrem = $listlen % $p;
    $partition = array();
    $mark = 0;
    for($px = 0; $px < $p; $px ++) {
        $incr = ($px < $partrem) ? $partlen + 1 : $partlen;
        $partition[$px] = array_slice($list, $mark, $incr);
        $mark += $incr;
    }
    return $partition;
}    

Use it like this:

$array = array();
$array = range(1, 10);
$number_of_split = 3;
$chunks = partition($array, $number_of_split);
print_r($chunks);

solved PHP Function to devide an array of N elements in N sub array