Backend Development 4 min read

How to Use PHP's array_chunk() Function to Split Arrays

This article explains the PHP array_chunk() function, its syntax, parameters, and provides clear code examples showing how to split a large array into smaller chunks with optional key preservation for effective backend array handling.

php中文网 Courses
php中文网 Courses
php中文网 Courses
How to Use PHP's array_chunk() Function to Split Arrays

In PHP development, handling arrays is a common task, and sometimes you need to split a large array into several smaller arrays of a specified size, which is where the array_chunk() function comes into play. This article details the usage of array_chunk() and provides several code examples.

The syntax of array_chunk() is as follows:

array array_chunk ( array $array , int $size [, bool $preserve_keys = false ] )

The function accepts three parameters:

$array – the array to be split.

$size – the size of each resulting chunk.

$preserve_keys – a boolean indicating whether to preserve the original keys in the chunks.

Below is a simple example that splits an array into chunks of size 3:

<?php
$array = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j');
$chunks = array_chunk($array, 3);
print_r($chunks);
?>

The original array contains 10 elements. Calling array_chunk() with $size set to 3 produces four chunks, the last one containing a single element. The output is:

Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
            [2] => c
        )

    [1] => Array
        (
            [0] => d
            [1] => e
            [2] => f
        )

    [2] => Array
        (
            [0] => g
            [1] => h
            [2] => i
        )

    [3] => Array
        (
            [0] => j
        )

)

By default, the keys are reindexed. To keep the original keys, set the third parameter $preserve_keys to true. The following example demonstrates this:

<?php
$array = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5, 'f' => 6);
$chunks = array_chunk($array, 2, true);
print_r($chunks);
?>

Running the code yields:

Array
(
    [0] => Array
        (
            [a] => 1
            [b] => 2
        )

    [1] => Array
        (
            [c] => 3
            [d] => 4
        )

    [2] => Array
        (
            [e] => 5
            [f] => 6
        )

)

This shows that the original array has been successfully split into three smaller arrays while preserving the original keys.

In summary, the array_chunk() function is a practical PHP array utility that can divide a large array into multiple smaller arrays and optionally retain the original keys, making it valuable for handling sizable data structures in backend development.

Backend Developmentphpphp-functionsarray_chunkarray splitting
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.