Last active
April 17, 2016 11:39
-
-
Save Ambalus/91731c650874d18777fce0459d554e97 to your computer and use it in GitHub Desktop.
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 | |
| function array_chunk_min($list,$minSize){ | |
| $cnt = count($list); | |
| if($cnt <= $minSize){ | |
| return [$list]; | |
| } | |
| $lastChunkSize = $cnt%$minSize; | |
| if($lastChunkSize > 0){ | |
| $chunks = array_chunk(array_slice($list, 0, $cnt-$lastChunkSize),$minSize); | |
| $lastChunk = array_slice($list, $cnt-$lastChunkSize,$minSize); | |
| $cntChunks = count($chunks); | |
| $chunks[$cntChunks-1] = array_merge($chunks[$cntChunks-1],$lastChunk); | |
| }else{ | |
| $chunks = array_chunk($list, $minSize); | |
| } | |
| return $chunks; | |
| } | |
| print_r( | |
| array_chunk_min( | |
| range(1,75),20 | |
| ) | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment