3v4l.org

run code in 300+ PHP versions simultaneously
<?php function array_chunk_vertical($data, $columns = 2) { $n = count($data) ; $per_column = floor($n / $columns) ; $rest = $n % $columns ; // The map $per_columns = array( ) ; for ( $i = 0 ; $i < $columns ; $i++ ) { $per_columns[$i] = $per_column + ($i < $rest ? 1 : 0) ; } $tabular = array( ) ; foreach ( $per_columns as $rows ) { for ( $i = 0 ; $i < $rows ; $i++ ) { $tabular[$i][ ] = array_shift($data) ; } } return $tabular ; } $cars = [ [ 'make' => 'Alfa Romeo', 'id' => 2 ], [ 'make' => 'Aston Martin', 'id' => 3 ], [ 'make' => 'Audi', 'id' => 4 ], [ 'make' => 'BMW', 'id' => 8 ], [ 'make' => 'Caterham', 'id' => 9 ], ]; echo '<table>'; foreach(array_chunk_vertical($cars,4) as $row) { echo '<tr>'; foreach($row as $car) { echo '<td><input type="checkbox" value="', $car['id'], '" />', $car['make'], '</td>'; } echo '</tr>'; } echo '</table>';

preferences:
55.9 ms | 402 KiB | 5 Q