PHP array_chunk() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_chunk() splits an array into smaller arrays of a fixed size. It returns a new array where each item is one of these smaller pieces, called chunks.

You use array_chunk() for things like pagination, building grid rows or sending data in batches. Every chunk has the length you ask for, except that the last chunk may be shorter when the items do not divide evenly.

Syntax

Call array_chunk() with the values you need. Start with this line.

PHP
$pages = array_chunk($items, 3);
CallWhat it does
array_chunk([1, 2, 3, 4, 5], 2)Returns three chunks: [1, 2], [3, 4] and [5].
array_chunk($scores, 2, true)Returns chunks that keep the original keys of each item.
array_chunk([1, 2, 3], 10)Returns one chunk that holds all three items, because the length is bigger than the array.

By default each chunk is re-indexed from 0. Pass true as the third argument when the keys matter, for example with an associative array. The original array is not changed.

Library declarations
PHP
array_chunk(array $array, int $length, bool $preserve_keys = false): array

Parameters

ParameterDescription
arrayRequired. The array to split.
lengthRequired. The size of each chunk. It must be 1 or more.
preserve_keysOptional. When true, the original keys are kept inside each chunk. When false (the default) each chunk gets new keys starting at 0.

Return Value

Return ValueDescription
arrayA list of chunks. Each chunk is an array. An empty input array gives an empty array.

PHP Examples

Example 1 — Splitting a List Into Chunks

Split five letters into chunks of two and print each chunk.

PHP
<?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

  1. array_chunk($letters, 2) cuts the list into pieces of two items.
  2. The first two chunks are full and hold two letters each.
  3. Only 'e' is left over, so the last chunk holds one item.
  4. The loop prints each chunk number together with its items.

Example 2 — Keeping the Original Keys

With an associative array, preserve_keys decides whether the original keys survive inside the chunks.

PHP
<?php
$scores = ['x' => 1, 'y' => 2, 'z' => 3];
$plain = array_chunk($scores, 2);
$kept = array_chunk($scores, 2, true);

echo implode(',', array_keys($plain[0])), PHP_EOL;
echo implode(',', array_keys($kept[0])), PHP_EOL;
echo implode(',', array_keys($kept[1])), PHP_EOL;

How It Works

  1. Without the third argument each chunk is re-indexed, so the first chunk has keys 0 and 1.
  2. The names 'x' and 'y' are lost in that first result.
  3. With true as the third argument the first chunk keeps its keys 'x' and 'y'.
  4. The second chunk holds the single leftover item with its key 'z'.

Example 3 — Simple Pagination

Split a list into pages of three items, then read a page by its index.

PHP
<?php
$items = range(1, 7);
$pages = array_chunk($items, 3);

echo "Pages: ", count($pages), PHP_EOL;
echo "Page 2: ", implode(', ', $pages[1]), PHP_EOL;
echo "Last page: ", implode(', ', $pages[2]), PHP_EOL;

How It Works

  1. range(1, 7) creates the numbers 1 to 7.
  2. array_chunk() makes pages of 3, 3 and 1 items.
  3. count($pages) gives the number of pages, which is 3.
  4. Page indexes start at 0, so $pages[1] is the second page and $pages[2] is the short last page.

Frequently Asked Questions

It splits an array into smaller arrays of a given size. The result is an array of chunks, and the last chunk can be shorter than the rest.
When it is true, each chunk keeps the keys that its items had in the original array. When it is false, which is the default, every chunk is numbered from 0.
PHP 8 throws a ValueError because the chunk size must be at least 1. Check the value before you call array_chunk() when it comes from user input.
It returns an empty array, because there is nothing to split.
Chunk the full list by the page size, then pick a page by its index, for example $pages[$pageNumber - 1]. For large data sets, array_slice() or a database LIMIT is usually more efficient.

Next: array_column()

array_column() takes a list of rows, such as records from a database, and returns the valu.

array_column() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful