Example 1 — Splitting a List Into Chunks
Split five letters into chunks of two and print each chunk.
<?php
$letters = ['a', 'b', 'c', 'd', 'e'];
$chunks = array_chunk($letters, 2);
foreach ($chunks as $i => $chunk) {
echo "Chunk $i: ", implode(', ', $chunk), PHP_EOL;
} How It Works
- array_chunk($letters, 2) cuts the list into pieces of two items.
- The first two chunks are full and hold two letters each.
- Only 'e' is left over, so the last chunk holds one item.
- The loop prints each chunk number together with its items.

