Current Location: Home> Latest Articles> Does array_chunk support nested splitting of multidimensional arrays?

Does array_chunk support nested splitting of multidimensional arrays?

M66 2025-04-26

In PHP, array_chunk() is a very common array function. It can divide an array into multiple small arrays, and the number of elements in each small array is specified by the user. However, for a multi-dimensional array, array_chunk() does not recursively split the nested arrays in by default. It will split the outermost array by the number of elements passed in.

Basic usage of array_chunk

The syntax of array_chunk() is very simple, it accepts the following parameters:

 array_chunk(array $array, int $length, bool $preserve_keys = false): array
  • $array : an array that needs to be split.

  • $length : The number of elements each small array contains.

  • $preserve_keys : Whether to preserve the key name of the original array. The default is false and the key name will be reindexed.

For example, the following code splits a one-dimensional array into multiple small arrays:

 $array = [1, 2, 3, 4, 5, 6, 7, 8, 9];
$result = array_chunk($array, 3);

print_r($result);

Output result:

 Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

    [1] => Array
        (
            [0] => 4
            [1] => 5
            [2] => 6
        )

    [2] => Array
        (
            [0] => 7
            [1] => 8
            [2] => 9
        )
)

As shown above, array_chunk() splits the $array array into three small arrays, each small array containing up to three elements.

The situation of multidimensional arrays

When an array is multidimensional, the default behavior of array_chunk() is to split only the outermost array without recursively processing the nested arrays therein. If you want to nest splitting a multidimensional array, array_chunk() cannot directly achieve this goal.

Example:

Suppose we have a two-dimensional array, like this:

 $array = [
    ['apple', 'banana', 'cherry'],
    ['dog', 'elephant', 'fox'],
    ['grape', 'honeydew', 'kiwi']
];

If we use array_chunk() to split this two-dimensional array:

 $result = array_chunk($array, 2);

print_r($result);

The output will be:

 Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => apple
                    [1] => banana
                    [2] => cherry
                )

            [1] => Array
                (
                    [0] => dog
                    [1] => elephant
                    [2] => fox
                )
        )

    [1] => Array
        (
            [0] => Array
                (
                    [0] => grape
                    [1] => honeydew
                    [2] => kiwi
                )
        )
)

As shown above, when array_chunk() splits a two-dimensional array, it does not further split the nested array, but instead splits the outermost array into two small arrays. Each small array still contains a complete subarray.

How to recursively split a multidimensional array?

To implement recursive splitting of multidimensional arrays, we can simulate the behavior of array_chunk() through custom recursive functions. This can split the nested array by traversing the array and calling array_chunk() recursively.

Here is an example of recursively splitting multidimensional arrays:

 function recursive_array_chunk($array, $length) {
    $result = [];
    foreach ($array as $key => $value) {
        // If the value is an array,Recursive splitting
        if (is_array($value)) {
            $result[$key] = recursive_array_chunk($value, $length);
        } else {
            $result[] = $value;
        }
    }

    return array_chunk($result, $length);
}

$array = [
    ['apple', 'banana', 'cherry'],
    ['dog', 'elephant', 'fox'],
    ['grape', 'honeydew', 'kiwi']
];

$result = recursive_array_chunk($array, 2);
print_r($result);

The recursive_array_chunk() function recursively processes each layer in a multidimensional array, thereby realizing the splitting of a multidimensional array. Eventually, the nested arrays are also correctly split.

summary

  • array_chunk() defaults to splitting multi-dimensional arrays only handles the outermost array, and nested arrays will not be processed recursively.

  • If you want to recursively split a multidimensional array, you need a custom function to implement it.

  • The array can be split layer by layer through recursion to ensure that the nested arrays in multi-dimensional arrays can also be split.

I hope this article can help you understand the application of array_chunk() in multi-dimensional arrays. If you have other questions or need more in-depth analysis, please visit our website!