Skip to content

Instantly share code, notes, and snippets.

@hakre
Created February 25, 2012 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hakre/1910251 to your computer and use it in GitHub Desktop.
Save hakre/1910251 to your computer and use it in GitHub Desktop.
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