Skip to content

Instantly share code, notes, and snippets.

@mitchkramez
Created January 17, 2012 15:32
Show Gist options
  • Save mitchkramez/1627079 to your computer and use it in GitHub Desktop.
Save mitchkramez/1627079 to your computer and use it in GitHub Desktop.
sort an array for output in columns
/**
* function: array_columns
* author: Brecht Cloetens
* params: $a = array() // original array
* $c = int() // number of columns
*/
function array_columns(&$a, $c=2)
{
$m = ceil(count($a)/$c);
$j = 0;
for($i=0; $i<$m; $i++) {
for($k=0; $k<$c; $k++) {
$key = $i+($m*$k);
settype($key,'integer');
if(array_key_exists($key,$a)) {
$b[$j] = $a[$key];
$j++;
}
}
}
$a = $b;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment