3v4l.org

run code in 300+ PHP versions simultaneously
<?php $matrix = [ [0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11], ]; /* i from to i/col i%col i&col i/rows 0 = [0][0] => [0][0] 0 0 0 0 1 = [0][1] => [1][0] 0.3 1 1 0.25 2 = [0][2] => [2][0] 0.6 2 2 0.5 3 = [1][0] => [3][0] 1 0 3 0.75 4 = [1][1] => [0][1] 1.3 1 0 1 5 = [1][2] => [1][1] 1.6 2 1 1.25 6 = [2][0] => [2][1] 2 0 2 1.5 7 = [2][1] => [3][1] 2.3 1 3 1.75 8 = [2][2] => [0][2] 2.6 2 0 2 9 = [3][0] => [1][2] 3 0 1 2.25 10 = [3][1] => [2][2] 3.3 1 2 2.5 11 = [3][2] => [3][2] 3.6 2 3 2.75 */ $rows = count($matrix); $cols = count(current($matrix)); $cells = $rows * $cols; $result = $matrix; for ($i = 0; $i < $cells; ++$i) { $result[$i % $rows][$i / $rows] = $matrix[$i / $cols][$i % $cols]; } var_export($result);
Output for 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.28, 8.4.1 - 8.4.14, 8.5.0
Deprecated: Implicit conversion from float 0.3333333333333333 to int loses precision in /in/LJ7LT on line 30 Deprecated: Implicit conversion from float 0.25 to int loses precision in /in/LJ7LT on line 30 Deprecated: Implicit conversion from float 0.6666666666666666 to int loses precision in /in/LJ7LT on line 30 Deprecated: Implicit conversion from float 0.5 to int loses precision in /in/LJ7LT on line 30 Deprecated: Implicit conversion from float 0.75 to int loses precision in /in/LJ7LT on line 30 Deprecated: Implicit conversion from float 1.3333333333333333 to int loses precision in /in/LJ7LT on line 30 Deprecated: Implicit conversion from float 1.6666666666666667 to int loses precision in /in/LJ7LT on line 30 Deprecated: Implicit conversion from float 1.25 to int loses precision in /in/LJ7LT on line 30 Deprecated: Implicit conversion from float 1.5 to int loses precision in /in/LJ7LT on line 30 Deprecated: Implicit conversion from float 2.3333333333333335 to int loses precision in /in/LJ7LT on line 30 Deprecated: Implicit conversion from float 1.75 to int loses precision in /in/LJ7LT on line 30 Deprecated: Implicit conversion from float 2.6666666666666665 to int loses precision in /in/LJ7LT on line 30 Deprecated: Implicit conversion from float 2.25 to int loses precision in /in/LJ7LT on line 30 Deprecated: Implicit conversion from float 3.3333333333333335 to int loses precision in /in/LJ7LT on line 30 Deprecated: Implicit conversion from float 2.5 to int loses precision in /in/LJ7LT on line 30 Deprecated: Implicit conversion from float 3.6666666666666665 to int loses precision in /in/LJ7LT on line 30 Deprecated: Implicit conversion from float 2.75 to int loses precision in /in/LJ7LT on line 30 array ( 0 => array ( 0 => 0, 1 => 4, 2 => 8, ), 1 => array ( 0 => 1, 1 => 5, 2 => 9, ), 2 => array ( 0 => 2, 1 => 6, 2 => 10, ), 3 => array ( 0 => 3, 1 => 7, 2 => 11, ), )
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.
Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
array ( 0 => array ( 0 => 0, 1 => 4, 2 => 8, ), 1 => array ( 0 => 1, 1 => 5, 2 => 9, ), 2 => array ( 0 => 2, 1 => 6, 2 => 10, ), 3 => array ( 0 => 3, 1 => 7, 2 => 11, ), )

preferences:
230.79 ms | 412 KiB | 5 Q