Vi Veri Veniversum Vivus Vici
<?php $r1 = rand(0, 7); $r2 = rand(0, 7); $a = []; for ($i = 0; $i < $r1; $i++) { for ($j = 0; $j < $r2; $j++) { $a[$i][$j] = rand(1, 10); } } $r = []; for ($i = 0; $i < $r1; $i++) { $t = []; for ($j = 0; $j < $r2; $j++) { $t[] = $a[$i][$j]; } $r[] = '[' . implode(', ', $t) . ']'; } echo "[\n" . implode(",\n", $r) . "\n];\n\n"; //var_dump([$r1, $r2]); /**$a = [ [6, 10, 9], [3, 5, 2], [8, 2, 5], [4, 1, 8], [6, 10, 5], [6, 8, 10] ]; */ /** $a = [ [1, 2, 3, 4], [14, 15, 16, 5], [13, 20, 17, 6], [12, 19, 18, 7], [11, 10, 9, 8] ];*/ $rows = sizeof($a); if ($rows) { $cols = sizeof($a[0]); } else { $cols = 0; } $total = $rows * $cols; $i = $cols; $d = 'cols'; $j = 0; $c = 0; $x = 0; $y = 0; $dy = 0; $dx = 1; $r = []; while ($c < $total) { $r[] = $a[$y][$x]; $j++; if ($j === $i) { $j = 0; list($dx, $dy) = [-$dy, $dx]; if ($d === 'cols') { $rows--; $i = $rows; $d = 'rows'; } else { $cols--; $i = $cols; $d = 'cols'; } } $x = $x + $dx; $y = $y + $dy; $c++; } if (empty($r)) echo "Empty result\n"; echo implode(' ', $r);