简体   繁体   中英

2D Rotation Array

I need help converting this code from this thread: How to Rotate a 2D Array of Integers

to PHP arrays.

int [,] newArray = new int[4,4];


for (int i=3;i>=0;--i) {
    for (int j=0;j<4;++j) {
        newArray[j,3-i] = array[i,j];
    }
}

Also, will this code work if the blocks are off-center?

Without putting any thought into this at all, "jagged arrays" are pretty much identical to "rectangular arrays" (which PHP doesn't support), so use em:

$newArray = array(array())

for($i=3; $i >= 0; --$i) {
    for($j=0; $j < 4; ++$j) {
        $newArray[$j][3-$i] = $oldArray[$i][$j];
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM