1

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?

Community
  • 1
  • 1
atomical
  • 297
  • 2
  • 5
  • 12

1 Answers1

0

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];
    }
}
mpen
  • 272,448
  • 266
  • 850
  • 1,236