[ create a new paste ] login | about

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

PHP, pasted on Feb 25:
<?php

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

$len = count($array);
$col = $len / $row;

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

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

function print_matrix($matrix, $perRow)
{
    print_r($matrix);

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


Output:
Original:
Array
(
    [0] => A
    [1] => B
    [2] => C
    [3] => D
    [4] => E
    [5] => F
    [6] => G
    [7] => H
    [8] => I
    [9] => J
    [10] => K
    [11] => L
    [12] => M
    [13] => N
    [14] => O
    [15] => P
    [16] => Q
    [17] => R
    [18] => S
    [19] => T
    [20] => U
    [21] => V
    [22] => W
    [23] => X
    [24] => Y
    [25] => 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:
Array
(
    [0] => A
    [1] => G
    [2] => N
    [3] => T
    [4] => B
    [5] => H
    [6] => O
    [7] => U
    [8] => C
    [9] => I
    [10] => P
    [11] => V
    [12] => D
    [13] => J
    [14] => Q
    [15] => W
    [16] => E
    [17] => K
    [18] => R
    [19] => X
    [20] => F
    [21] => L
    [22] => S
    [23] => 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: