如何在PHP中旋转二维数组90度

时间:2021-05-24 09:21:04

I want to rotate a matrix 90 degrees clockwise. This amounts to making the first column in the input the first row of the output, the second column of the input the 2nd row of the output, and the 3rd column of the input the 3rd row of the output. Note that the bottom of the column=the beginning of the row, because of the 90 degree rotation.

我想顺时针旋转90度矩阵。这相当于使输入中的第一列成为输出的第一行,输入的第二列成为输出的第二行,输入的第三列成为输出的第3行。请注意,列的底部=行的开头,因为旋转90度。

For example:

例如:

$matrix=    [[1, 2, 3]
             [4, 5, 6], 
             [7, 8, 9]];

rotate90degrees($matrix)=      [[7, 4, 1],
                                [8, 5, 2],
                                [9, 6, 3]]

What I know is I have first transpose the matrix and then swap the columns to rotate the matrix by 90 degrees. How can this be applied to php?

我所知道的是我首先转置矩阵,然后交换列以将矩阵旋转90度。怎么能将这个应用到php?

3 个解决方案

#1


2  

php doesn't have concepts like "transpose" for a matrix without adding some sort of linear algebra library. you can do it natively by eaching through the matrix and swapping some indexes

php没有像矩阵的“转置”这样的概念而没有添加某种线性代数库。你可以通过遍历矩阵并交换一些索引来原生地完成它

<?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

#2


7  

I showed you how to transpose an array in answer to a previous question, to rotate 90 degrees, use that transpose logic, and then reverse the order of the values in each row in turn:

我向您展示了如何转换数组以回答上一个问题,旋转90度,使用该转置逻辑,然后依次颠倒每行中值的顺序:

$matrix = [
    [1, 2, 3],
    [4, 5, 6], 
    [7, 8, 9],
];

array_unshift($matrix, null);
$matrix = call_user_func_array('array_map', $matrix);
$matrix = array_map('array_reverse', $matrix);
var_dump($matrix);

Demo

演示

#3


2  

Another reliable option:

另一个可靠选择:

function rotateMatrix90( $matrix )
{
    $matrix = array_values( $matrix );
    $matrix90 = array();

    // make each new row = reversed old column
    foreach( array_keys( $matrix[0] ) as $column ){
        $matrix90[] = array_reverse( array_column( $matrix, $column ) );
    }

    return $matrix90;
}

Less clever than @mark-baker's by far. Maybe more clear.

到目前为止,比@ mark-baker更不聪明。也许更清楚。

#1


2  

php doesn't have concepts like "transpose" for a matrix without adding some sort of linear algebra library. you can do it natively by eaching through the matrix and swapping some indexes

php没有像矩阵的“转置”这样的概念而没有添加某种线性代数库。你可以通过遍历矩阵并交换一些索引来原生地完成它

<?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

#2


7  

I showed you how to transpose an array in answer to a previous question, to rotate 90 degrees, use that transpose logic, and then reverse the order of the values in each row in turn:

我向您展示了如何转换数组以回答上一个问题,旋转90度,使用该转置逻辑,然后依次颠倒每行中值的顺序:

$matrix = [
    [1, 2, 3],
    [4, 5, 6], 
    [7, 8, 9],
];

array_unshift($matrix, null);
$matrix = call_user_func_array('array_map', $matrix);
$matrix = array_map('array_reverse', $matrix);
var_dump($matrix);

Demo

演示

#3


2  

Another reliable option:

另一个可靠选择:

function rotateMatrix90( $matrix )
{
    $matrix = array_values( $matrix );
    $matrix90 = array();

    // make each new row = reversed old column
    foreach( array_keys( $matrix[0] ) as $column ){
        $matrix90[] = array_reverse( array_column( $matrix, $column ) );
    }

    return $matrix90;
}

Less clever than @mark-baker's by far. Maybe more clear.

到目前为止,比@ mark-baker更不聪明。也许更清楚。