Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
flip orientation
<?php
/**
* @link http://stackoverflow.com/q/9445369/367456
*/
$array = range('A', 'M');
$columns = 4;
$length = count($array);
print_matrix($array, $columns);
$floor = floor($length/$columns);
$modulo = $length % $columns;
$max = $length-1;
$virtual = 0;
$keys = array_keys($array);
$build = array();
foreach($keys as $index => $key)
{
$vkey = $keys[$virtual];
$build[$vkey] = $array[$vkey];
$virtual += $floor + ($index % $columns < $modulo);
($virtual>$max) && $virtual %= $max;
}
print_matrix($build, $columns);
function print_matrix($matrix, $columns)
{
echo "One row - " . implode(' ', $matrix) . "\n";
foreach(array_chunk($matrix, $columns, 1) as $row)
{
foreach($row as $key => $col)
printf('%s[%2d] ', $col, $key);
echo "\n";
}
echo "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.