array_chunk
array_chunk(array $array, int $length, bool $preserve_keys = false): array
Splits an array into chunks of a fixed length.
C C23
#include <stdio.h>
int main(void) {
int xs[] = {1, 2, 3, 4, 5};
int n = 5, size = 2;
for (int start = 0; start < n; start += size) {
int end = start + size < n ? start + size : n;
printf("[");
for (int i = start; i < end; i++) {
printf("%d%s", xs[i], i + 1 < end ? "," : "");
}
printf("]%s", start + size < n ? " " : "\n");
}
return 0;
}
[1,2] [3,4] [5]
C++ C++26
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> xs{1, 2, 3, 4, 5};
size_t size = 2;
for (size_t start = 0; start < xs.size(); start += size) {
size_t end = std::min(start + size, xs.size());
std::cout << "[";
for (size_t i = start; i < end; i++) {
std::cout << xs[i] << (i + 1 < end ? "," : "");
}
std::cout << "]" << (start + size < xs.size() ? " " : "\n");
}
return 0;
}
[1,2] [3,4] [5]
C# v11.0.100
foreach (int[] chunk in new[] { 1, 2, 3, 4, 5 }.Chunk(2)) {
Console.WriteLine($"[{string.Join(",", chunk)}]");
}
[1,2] [3,4] [5]
Go v1.27.4
package main
import "fmt"
func main() {
xs := []int{1, 2, 3, 4, 5}
size := 2
for start := 0; start < len(xs); start += size {
end := min(start+size, len(xs))
fmt.Println(xs[start:end])
}
}
[1 2] [3 4] [5]
Java v27.0.1
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> xs = List.of(1, 2, 3, 4, 5);
int size = 2;
for (int start = 0; start < xs.size(); start += size) {
int end = Math.min(start + size, xs.size());
System.out.println(xs.subList(start, end)); // subList is a view — no data copying
}
}
}
[1, 2] [3, 4] [5]
JavaScript v26.13.1
const xs = [1, 2, 3, 4, 5];
const chunks = [];
for (let i = 0; i < xs.length; i += 2) {
chunks.push(xs.slice(i, i + 2));
}
console.log(chunks);
[ [ 1, 2 ], [ 3, 4 ], [ 5 ] ]
PHP v8.6.0
echo json_encode(array_chunk([1, 2, 3, 4, 5], 2)), "\n",
json_encode(array_chunk([1, 2, 3, 4, 5], 2, true));
[[1,2],[3,4],[5]]
[[1,2],{"2":3,"3":4},{"4":5}]
Python v3.15.2
xs = [1, 2, 3, 4, 5]
print([xs[i:i + 2] for i in range(0, len(xs), 2)])
[[1, 2], [3, 4], [5]]
Rust v1.99.1
fn main() {
for chunk in [1, 2, 3, 4, 5].chunks(2) {
println!("{:?}", chunk);
}
}
[1, 2] [3, 4] [5]
TypeScript v26.13.1
const xs: number[] = [1, 2, 3, 4, 5];
const chunks: number[][] = [];
for (let i = 0; i < xs.length; i += 2) {
chunks.push(xs.slice(i, i + 2));
}
console.log(chunks);
[ [ 1, 2 ], [ 3, 4 ], [ 5 ] ]
Language differences
C: no container abstraction — a double loop slices by stride for printing (example); real chunking needs hand-managed buffers.
C++: the same double loop with std::min at the tail (example); the standard library has no ready-made chunk algorithm (views::chunk arrives only with C++23 ranges).
C#: .NET 6+ LINQ Enumerable.Chunk(2) supports it natively (example), returning a lazy sequence.
Go: a hand-written strided slice (example); xs[start:end] is a view with no data copy; min is built in since 1.21.
Java: no standard chunk utility — a strided loop over subList(start, end) (example) returns views (the comment stresses no copying); Guava's Lists.partition is the common third-party alternative.
JavaScript: no built-in chunk — a slice strided loop is the most universal form (example); Object.groupBy/Map.groupBy group by key, not fixed length.
PHP: preserve_keys = true keeps the original keys (second line of the example: chunks whose keys stay consecutive from 0 still encode as arrays, chunks starting at key 2 encode as objects — a JSON encoding rule); element order within chunks is unchanged; the last chunk may be shorter than $length.
Python: a slicing one-liner (example); note itertools.batched (3.12+) is the standard-library chunking iterator.
Rust: slices ship chunks(2) natively (example) — guaranteed equal-length except the last; chunks_exact drops the remainder instead.
TypeScript: same slice loop as JavaScript (example), with number[][] at the type level.