3v4l.org

run code in 300+ PHP versions simultaneously
<?php $input = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], ]; $output = array(); $intern = array(); for($row=0; $row < 4; $row++) for($col=0;$col < 3;$col++) $intern[] = $input[$row][$col]; // nesting the array $count = 0; $subcount = 0; foreach($intern as $value) { $output[$count][$subcount] = $value; $count++; if($subcount == 3) { break; } if($count == 4) { $count = 0; $subcount++; } } var_export($output);

preferences:
32.56 ms | 404 KiB | 5 Q