3v4l.org

run code in 300+ PHP versions simultaneously
<?php function littleEndianMinChunk(array $array, int $minChunkSize): array { if ($minChunkSize < 1) { throw new InvalidArgumentException('$minChunkSize must be 1 or greater'); } if ($minChunkSize < count($array)) { throw new InvalidArgumentException('$minChunkSize must not be less than the size of $array'); } $result = []; while ($array) { $count = count($array); $modulus = $count % $minChunkSize; if (!$modulus) { array_unshift($result, ...array_chunk($array, $minChunkSize)); break; } $expectedRemainingChunks = max(1, intdiv($count, $minChunkSize)); $sizeIncrease = ceil($modulus / $expectedRemainingChunks); array_unshift($result, array_splice($array, -$minChunkSize - $sizeIncrease)); } return $result; } $arraySize = 13; for ($n = 1; $n <= $arraySize; ++$n) { echo "arraySize $n \n"; $array = range(1, $n); for ($x = 1; $x <= 13; ++$x) { echo "chunkSize $x: "; try { echo json_encode(littleEndianMinChunk($array, $x)) . "\n"; } catch (Exception $e) { echo $e->getMessage() . "\n"; } } }
Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
arraySize 1 chunkSize 1: [[1]] chunkSize 2: [[1]] chunkSize 3: [[1]] chunkSize 4: [[1]] chunkSize 5: [[1]] chunkSize 6: [[1]] chunkSize 7: [[1]] chunkSize 8: [[1]] chunkSize 9: [[1]] chunkSize 10: [[1]] chunkSize 11: [[1]] chunkSize 12: [[1]] chunkSize 13: [[1]] arraySize 2 chunkSize 1: $minChunkSize must not be less than the size of $array chunkSize 2: [[1,2]] chunkSize 3: [[1,2]] chunkSize 4: [[1,2]] chunkSize 5: [[1,2]] chunkSize 6: [[1,2]] chunkSize 7: [[1,2]] chunkSize 8: [[1,2]] chunkSize 9: [[1,2]] chunkSize 10: [[1,2]] chunkSize 11: [[1,2]] chunkSize 12: [[1,2]] chunkSize 13: [[1,2]] arraySize 3 chunkSize 1: $minChunkSize must not be less than the size of $array chunkSize 2: $minChunkSize must not be less than the size of $array chunkSize 3: [[1,2,3]] chunkSize 4: [[1,2,3]] chunkSize 5: [[1,2,3]] chunkSize 6: [[1,2,3]] chunkSize 7: [[1,2,3]] chunkSize 8: [[1,2,3]] chunkSize 9: [[1,2,3]] chunkSize 10: [[1,2,3]] chunkSize 11: [[1,2,3]] chunkSize 12: [[1,2,3]] chunkSize 13: [[1,2,3]] arraySize 4 chunkSize 1: $minChunkSize must not be less than the size of $array chunkSize 2: $minChunkSize must not be less than the size of $array chunkSize 3: $minChunkSize must not be less than the size of $array chunkSize 4: [[1,2,3,4]] chunkSize 5: [[1,2,3,4]] chunkSize 6: [[1,2,3,4]] chunkSize 7: [[1,2,3,4]] chunkSize 8: [[1,2,3,4]] chunkSize 9: [[1,2,3,4]] chunkSize 10: [[1,2,3,4]] chunkSize 11: [[1,2,3,4]] chunkSize 12: [[1,2,3,4]] chunkSize 13: [[1,2,3,4]] arraySize 5 chunkSize 1: $minChunkSize must not be less than the size of $array chunkSize 2: $minChunkSize must not be less than the size of $array chunkSize 3: $minChunkSize must not be less than the size of $array chunkSize 4: $minChunkSize must not be less than the size of $array chunkSize 5: [[1,2,3,4,5]] chunkSize 6: [[1,2,3,4,5]] chunkSize 7: [[1,2,3,4,5]] chunkSize 8: [[1,2,3,4,5]] chunkSize 9: [[1,2,3,4,5]] chunkSize 10: [[1,2,3,4,5]] chunkSize 11: [[1,2,3,4,5]] chunkSize 12: [[1,2,3,4,5]] chunkSize 13: [[1,2,3,4,5]] arraySize 6 chunkSize 1: $minChunkSize must not be less than the size of $array chunkSize 2: $minChunkSize must not be less than the size of $array chunkSize 3: $minChunkSize must not be less than the size of $array chunkSize 4: $minChunkSize must not be less than the size of $array chunkSize 5: $minChunkSize must not be less than the size of $array chunkSize 6: [[1,2,3,4,5,6]] chunkSize 7: [[1,2,3,4,5,6]] chunkSize 8: [[1,2,3,4,5,6]] chunkSize 9: [[1,2,3,4,5,6]] chunkSize 10: [[1,2,3,4,5,6]] chunkSize 11: [[1,2,3,4,5,6]] chunkSize 12: [[1,2,3,4,5,6]] chunkSize 13: [[1,2,3,4,5,6]] arraySize 7 chunkSize 1: $minChunkSize must not be less than the size of $array chunkSize 2: $minChunkSize must not be less than the size of $array chunkSize 3: $minChunkSize must not be less than the size of $array chunkSize 4: $minChunkSize must not be less than the size of $array chunkSize 5: $minChunkSize must not be less than the size of $array chunkSize 6: $minChunkSize must not be less than the size of $array chunkSize 7: [[1,2,3,4,5,6,7]] chunkSize 8: [[1,2,3,4,5,6,7]] chunkSize 9: [[1,2,3,4,5,6,7]] chunkSize 10: [[1,2,3,4,5,6,7]] chunkSize 11: [[1,2,3,4,5,6,7]] chunkSize 12: [[1,2,3,4,5,6,7]] chunkSize 13: [[1,2,3,4,5,6,7]] arraySize 8 chunkSize 1: $minChunkSize must not be less than the size of $array chunkSize 2: $minChunkSize must not be less than the size of $array chunkSize 3: $minChunkSize must not be less than the size of $array chunkSize 4: $minChunkSize must not be less than the size of $array chunkSize 5: $minChunkSize must not be less than the size of $array chunkSize 6: $minChunkSize must not be less than the size of $array chunkSize 7: $minChunkSize must not be less than the size of $array chunkSize 8: [[1,2,3,4,5,6,7,8]] chunkSize 9: [[1,2,3,4,5,6,7,8]] chunkSize 10: [[1,2,3,4,5,6,7,8]] chunkSize 11: [[1,2,3,4,5,6,7,8]] chunkSize 12: [[1,2,3,4,5,6,7,8]] chunkSize 13: [[1,2,3,4,5,6,7,8]] arraySize 9 chunkSize 1: $minChunkSize must not be less than the size of $array chunkSize 2: $minChunkSize must not be less than the size of $array chunkSize 3: $minChunkSize must not be less than the size of $array chunkSize 4: $minChunkSize must not be less than the size of $array chunkSize 5: $minChunkSize must not be less than the size of $array chunkSize 6: $minChunkSize must not be less than the size of $array chunkSize 7: $minChunkSize must not be less than the size of $array chunkSize 8: $minChunkSize must not be less than the size of $array chunkSize 9: [[1,2,3,4,5,6,7,8,9]] chunkSize 10: [[1,2,3,4,5,6,7,8,9]] chunkSize 11: [[1,2,3,4,5,6,7,8,9]] chunkSize 12: [[1,2,3,4,5,6,7,8,9]] chunkSize 13: [[1,2,3,4,5,6,7,8,9]] arraySize 10 chunkSize 1: $minChunkSize must not be less than the size of $array chunkSize 2: $minChunkSize must not be less than the size of $array chunkSize 3: $minChunkSize must not be less than the size of $array chunkSize 4: $minChunkSize must not be less than the size of $array chunkSize 5: $minChunkSize must not be less than the size of $array chunkSize 6: $minChunkSize must not be less than the size of $array chunkSize 7: $minChunkSize must not be less than the size of $array chunkSize 8: $minChunkSize must not be less than the size of $array chunkSize 9: $minChunkSize must not be less than the size of $array chunkSize 10: [[1,2,3,4,5,6,7,8,9,10]] chunkSize 11: [[1,2,3,4,5,6,7,8,9,10]] chunkSize 12: [[1,2,3,4,5,6,7,8,9,10]] chunkSize 13: [[1,2,3,4,5,6,7,8,9,10]] arraySize 11 chunkSize 1: $minChunkSize must not be less than the size of $array chunkSize 2: $minChunkSize must not be less than the size of $array chunkSize 3: $minChunkSize must not be less than the size of $array chunkSize 4: $minChunkSize must not be less than the size of $array chunkSize 5: $minChunkSize must not be less than the size of $array chunkSize 6: $minChunkSize must not be less than the size of $array chunkSize 7: $minChunkSize must not be less than the size of $array chunkSize 8: $minChunkSize must not be less than the size of $array chunkSize 9: $minChunkSize must not be less than the size of $array chunkSize 10: $minChunkSize must not be less than the size of $array chunkSize 11: [[1,2,3,4,5,6,7,8,9,10,11]] chunkSize 12: [[1,2,3,4,5,6,7,8,9,10,11]] chunkSize 13: [[1,2,3,4,5,6,7,8,9,10,11]] arraySize 12 chunkSize 1: $minChunkSize must not be less than the size of $array chunkSize 2: $minChunkSize must not be less than the size of $array chunkSize 3: $minChunkSize must not be less than the size of $array chunkSize 4: $minChunkSize must not be less than the size of $array chunkSize 5: $minChunkSize must not be less than the size of $array chunkSize 6: $minChunkSize must not be less than the size of $array chunkSize 7: $minChunkSize must not be less than the size of $array chunkSize 8: $minChunkSize must not be less than the size of $array chunkSize 9: $minChunkSize must not be less than the size of $array chunkSize 10: $minChunkSize must not be less than the size of $array chunkSize 11: $minChunkSize must not be less than the size of $array chunkSize 12: [[1,2,3,4,5,6,7,8,9,10,11,12]] chunkSize 13: [[1,2,3,4,5,6,7,8,9,10,11,12]] arraySize 13 chunkSize 1: $minChunkSize must not be less than the size of $array chunkSize 2: $minChunkSize must not be less than the size of $array chunkSize 3: $minChunkSize must not be less than the size of $array chunkSize 4: $minChunkSize must not be less than the size of $array chunkSize 5: $minChunkSize must not be less than the size of $array chunkSize 6: $minChunkSize must not be less than the size of $array chunkSize 7: $minChunkSize must not be less than the size of $array chunkSize 8: $minChunkSize must not be less than the size of $array chunkSize 9: $minChunkSize must not be less than the size of $array chunkSize 10: $minChunkSize must not be less than the size of $array chunkSize 11: $minChunkSize must not be less than the size of $array chunkSize 12: $minChunkSize must not be less than the size of $array chunkSize 13: [[1,2,3,4,5,6,7,8,9,10,11,12,13]]

preferences:
62.79 ms | 418 KiB | 62 Q