fork download
  1. <?php
  2.  
  3. function transpose($array)
  4. {
  5. if (count($array) === 0) return $array;
  6. $out = [];
  7. foreach ($array as $rowkey => $row)
  8. {
  9. foreach($row as $colkey => $col)
  10. {
  11. $out[$colkey][$rowkey] = $col;
  12. }
  13. }
  14.  
  15. return $out;
  16. }
  17.  
  18. function f($a)
  19. {
  20. return (count($a) === 0)
  21. ? []
  22. f(array_reverse(transpose($a)))
  23. );
  24. }
  25.  
  26. print_r(f([[1,2,3,4],[10,11,12,5],[9,8,7,6]]));
Success #stdin #stdout 0.01s 82560KB
stdin
Standard input is empty
stdout
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
    [9] => 10
    [10] => 11
    [11] => 12
)