Created
January 7, 2016 19:23
-
-
Save themogwi/c81408d33ffd7e5c9584 to your computer and use it in GitHub Desktop.
Evenly distribute array items across specified number of columns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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