php does not have concepts like transpose for a matrix without adding any linear algebra library. you can do this initially by using a matrix and replacing some indices
<?php function rotate90($mat) { $height = count($mat); $width = count($mat[0]); $mat90 = array(); for ($i = 0; $i < $width; $i++) { for ($j = 0; $j < $height; $j++) { $mat90[$height - $i - 1][$j] = $mat[$height - $j - 1][$i]; } } return $mat90; } $mat = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; print_r($mat); //123 //456 //789 print_r(rotate90($mat)); //741 //852 //963 $mat = [[1, 2, 3], [4, 5, 6], [7, 8, 9], ["a", "b", "c"]]; print_r($mat); //123 //456 //789 //abc print_r(rotate90($mat)); //a741 //b852 //c963
source share