Created
March 11, 2016 07:54
-
-
Save aginev/027d9ace004321112252 to your computer and use it in GitHub Desktop.
Split array into a specific number of chuncks
This file contains hidden or 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 | |
| /** | |
| * Split array into a specific number of chuncks | |
| * | |
| * @param array $data | |
| * @param $chunks Number of chunks | |
| * @return array | |
| */ | |
| function partition(Array $data, $chunks) { | |
| $result = array(); | |
| $per_chunk = ceil(count($data) / $chunks); | |
| $counter = 0; | |
| foreach ($data as $row) { | |
| $result[floor($counter / $per_chunk)][] = $row; | |
| $counter++; | |
| } | |
| return $result; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Input:
Output: