Skip to content

Instantly share code, notes, and snippets.

@RodolVelasco
Created September 12, 2018 05:55
Show Gist options
  • Save RodolVelasco/12b4fe1c5a3e9900c8669eb1ae082472 to your computer and use it in GitHub Desktop.
Save RodolVelasco/12b4fe1c5a3e9900c8669eb1ae082472 to your computer and use it in GitHub Desktop.
PHP in multiple versions
Some php code
@RodolVelasco
Copy link
Author

Transforming rows to columns in "90 degrees"

Input

|1  |2  |3  |4  |
|5  |6  |7  |8  |
|9  |10 |11 |12 |
|13 |14 |15 |16 |

Output

|13 |9  |5 |1 |
|14 |10 |6 |2 |
|15 |11 |7 |3 |
|16 |12 |8 |4 |
$array = array(
              array(1,2,3,4),
              array(5,6,7,8),
              array(9,10,11,12),
              array(13,14,15,16)
         );
/* Pattern
[0][0]->[0][3]
[0][1]->[1][3]
[0][2]->[2][3]
[0][3]->[3][3]
[1][0]->[0][2]
[1][1]->[1][2]*/

$fila = count($array);
$columna = count($array[0]);
$nA = array(array(0,0,0,0),array(0,0,0,0),array(0,0,0,0),array(0,0,0,0));
$fixedIndex = $columna - 1;

for($i=0; $i < $fila; $i++){
    for($k=0; $k < $columna; $k++){
        $nA[$k][$fixedIndex] = $array[$i][$k];
        echo "fila: ".$i." | columna: ".$fixedIndex." = ".$nA[$i][$fixedIndex] . PHP_EOL;
    }
    $fixedIndex--;
}
echo $array[0][0].','.$array[0][1].','.$array[0][2].','.$array[0][3]. PHP_EOL;
echo $array[1][0].','.$array[1][1].','.$array[1][2].','.$array[1][3]. PHP_EOL;
echo $array[2][0].','.$array[2][1].','.$array[2][2].','.$array[2][3]. PHP_EOL;
echo $array[3][0].','.$array[3][1].','.$array[3][2].','.$array[3][3]. PHP_EOL;
echo PHP_EOL;
echo $nA[0][0].','.$nA[0][1].','.$nA[0][2].','.$nA[0][3]. PHP_EOL;
echo $nA[1][0].','.$nA[1][1].','.$nA[1][2].','.$nA[1][3]. PHP_EOL;
echo $nA[2][0].','.$nA[2][1].','.$nA[2][2].','.$nA[2][3]. PHP_EOL;
echo $nA[3][0].','.$nA[3][1].','.$nA[3][2].','.$nA[3][3]. PHP_EOL;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment