Skip to content

Instantly share code, notes, and snippets.

@themogwi
Created January 7, 2016 19:23
Show Gist options
  • Save themogwi/c81408d33ffd7e5c9584 to your computer and use it in GitHub Desktop.
Save themogwi/c81408d33ffd7e5c9584 to your computer and use it in GitHub Desktop.
Evenly distribute array items across specified number of columns
<?php
/**
* Evenly distribute array items across specified number of columns
*
* @param array $array An array of items to distribute
* @param integer $columns Number of columns
* @return array
*/
public function array_distribute($array, $columns) {
$distributed = array_fill(0, $columns, []);
while (!empty($array)) {
array_push($distributed[key($distributed)], array_shift($array));
if (next($distributed) === false) {
reset($distributed);
}
}
return $distributed;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment