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 (count($array) < $minChunkSize) { 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"; } } }

preferences:
16.01 ms | 411 KiB | 5 Q