Hi! Could we please enable some services and cookies to improve your experience and our website?
Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code
<?php
function rotate90degreesWithoutAdditionalSpace()
{
$matrix = [[1,2,3],
[4,5,6],
[7,8,9]];
$len = count($matrix);
// Swap the rows columns.
for($i=0; $i<$len; $i++) {
for($j=$i; $j<$len; $j++) {
$tmp = $matrix[$i][$j];
$matrix[$i][$j] = $matrix[$j][$i];
$matrix[$j][$i] = $tmp;
}
}
// Swap the elements from both ends of each row until the centered element in the row.
for($i=0; $i<$len; $i++) {
for($j=0; $j<floor($len/2); $j++) {
$tmp = $matrix[$i][$j];
$matrix[$i][$j] = $matrix[$i][$len-1-$j];
$matrix[$i][$len-1-$j] = $tmp;
}
}
return $matrix;
}
<?php
$matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
];
var_export(array_map(fn() => array_reverse(func_get_args()), ...$matrix));
echo "\n---\n";
var_export(array_map(fn(...$col) => array_reverse($col), ...$matrix));