3v4l.org

run code in 200+ PHP versions simultaneously
<?php function double_chunk($array, $maxrows) { $size = sizeof($array); $columns = ceil($size / $maxrows); $fullrows = $size - ($columns - 1) * $maxrows; if ($fullrows == $maxrows) { return array_chunk($array, $fullrows); // all columns have full rows, don't splice } return array_merge( array_chunk( array_splice($array, 0, $columns * $fullrows), $columns ), array_chunk($array, $columns - 1) ); } var_export(double_chunk(range('a', 'g'), 4));
based on gQnt7
Output for 5.6.38 - 7.4.0
array ( 0 => array ( 0 => 'a', 1 => 'b', ), 1 => array ( 0 => 'c', 1 => 'd', ), 2 => array ( 0 => 'e', 1 => 'f', ), 3 => array ( 0 => 'g', ), )