Current Location: Home> Latest Articles> Combine array_chunk with array_merge_recursive for merging array chunks

Combine array_chunk with array_merge_recursive for merging array chunks

M66 2025-04-27

How to use array_chunk with array_merge_recursive to implement the operation of chunking and then merging the array?

In PHP, array_chunk and array_merge_recursive are two very useful array manipulation functions. array_chunk can split an array into multiple small pieces, while array_merge_recursive allows multiple arrays to be merged together recursively. This article will show how to use these two functions in order to perform a merge operation after array chunking.

1. Use array_chunk for array chunking

array_chunk is a built-in function in PHP that can split an array into multiple small arrays. Its syntax is as follows:

 array_chunk(array $array, int $size, bool $preserve_keys = false): array
  • $array is an array that needs to be chunked.

  • $size is the size of each subarray.

  • The $preserve_keys parameter determines whether to retain the key value of the original array.

Sample code:

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

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
        )
)

2. Recursively merge arrays using array_merge_recursive

The array_merge_recursive function recursively combines multiple numbers into one. If a key in an array exists in multiple arrays, array_merge_recursive merges the values ​​into an array. Its syntax is as follows:

 array_merge_recursive(array ...$arrays): array
  • $arrays is the array to be merged, and can be any number of arrays.

Sample code:

 $array1 = ["a" => "apple", "b" => "banana"];
$array2 = ["a" => "apricot", "b" => "blueberry"];
$mergedArray = array_merge_recursive($array1, $array2);
print_r($mergedArray);

Output result:

 Array
(
    [a] => Array
        (
            [0] => apple
            [1] => apricot
        )

    [b] => Array
        (
            [0] => banana
            [1] => blueberry
        )
)

3. Combine array_chunk and array_merge_recursive for operations

We can use array_chunk and array_merge_recursive together, first chunking the large arrays, and then recursively merge each small array. This is very useful for some scenarios where data needs to be merged in chunks.

Suppose we have a multidimensional array, and we want to first divide it into several blocks and then merge those blocks. Here is an example of the implementation code:

 // Original array
$array = [
    "key1" => ["a" => "apple", "b" => "banana"],
    "key2" => ["a" => "apricot", "b" => "blueberry"],
    "key3" => ["c" => "cherry", "d" => "date"],
    "key4" => ["c" => "cranberry", "d" => "dragonfruit"]
];

// use array_chunk Block the array,Each block contains two elements
$chunkedArray = array_chunk($array, 2, true);

// use array_merge_recursive Merge each chunk
$mergedChunks = array();
foreach ($chunkedArray as $chunk) {
    $mergedChunks[] = array_merge_recursive(...$chunk);
}

// Output the merged result
print_r($mergedChunks);

Output result:

 Array
(
    [0] => Array
        (
            [key1] => Array
                (
                    [a] => apple
                    [b] => banana
                )

            [key2] => Array
                (
                    [a] => apricot
                    [b] => blueberry
                )
        )

    [1] => Array
        (
            [key3] => Array
                (
                    [c] => cherry
                    [d] => date
                )

            [key4] => Array
                (
                    [c] => cranberry
                    [d] => dragonfruit
                )
        )
)

4. Summary

By combining array_chunk and array_merge_recursive , we are able to easily divide arrays into chunks and merge them recursively. This approach is very effective for handling complex multi-dimensional arrays, especially when dealing with large amounts of data.