[ create a new paste ] login | about

Link: http://codepad.org/3cNMNYmT    [ raw code | output | fork ]

PHP, pasted on Feb 25:
<?php

$array = range('A', 'Z');
$row = 4;
echo "Original:\n";
print_matrix($array, $row);

echo "\nSorted:\n\n";
print_matrix(flipOrient($array, $row), $row);

function flipOrient($array, $rows)
{
    $len = count($array);
    $col = $len / $rows;

    $build = array();
    foreach(range(0, $col-1) as $x)
       foreach(range(0, $rows-1) as $y)
           $build[] = $array[$x + $y * $col];

    return $build;
}

function print_matrix($matrix, $perRow)
{
    echo "One row - " . implode(' ', $matrix) . "\n\n";

    foreach(array_chunk($matrix, $perRow) as $row)
    {
        foreach($row as $col)
            printf('%s ', $col);
        echo "\n";
    }
}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Original:
One row - A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

A B C D 
E F G H 
I J K L 
M N O P 
Q R S T 
U V W X 
Y Z 

Sorted:

One row - A G N T B H O U C I P V D J Q W E K R X F L S Y

A G N T 
B H O U 
C I P V 
D J Q W 
E K R X 
F L S Y 


Create a new paste based on this one


Comments: