Current Location: Home> Latest Articles> How to use array_chunk() and array_combine() functions to chunk and recombine?

How to use array_chunk() and array_combine() functions to chunk and recombine?

M66 2025-05-16

In PHP, the array_chunk() and array_combine() functions are common tools for processing arrays. array_chunk() can split a large array into multiple small arrays, while array_combine() allows you to recombine the keys and values ​​of an array into an associative array. These two functions are used in combination to allow us to process arrays easily, especially when we need to batch process data.

1. Use of array_chunk() function

The function of the array_chunk() function is to split a large array into multiple small arrays and return a new array according to the specified size. The basic syntax is as follows:

 array_chunk(array $array, int $size, bool $preserve_keys = false): array
  • $array : The original array that needs to be split.

  • $size : The size of each small array.

  • $preserve_keys : An optional parameter that determines whether to retain the keys of the original array. If true , the original key is retained; if false , the index is reind.

2. Use of array_combine() function

The array_combine() function is used to combine two arrays into an associative array. The basic syntax is as follows:

 array_combine(array $keys, array $values): array
  • $keys : an array containing keys.

  • $values : an array containing values.

array_combine() will use the elements in the $keys array as keys and the elements in the $values ​​array as values ​​to create a new associative array. If the length of the $keys and $values ​​arrays is different, false will be returned.

3. Use array_chunk() and array_combine() in combination

Suppose we have a large array with multiple key-value pairs, now we want to split the array into several small pieces and recombine them according to some rules. You can first use array_chunk() to chunk, and then use array_combine() to recombinate each array.

Sample code

 <?php
// Original array
$data = [
    'a' => 'apple',
    'b' => 'banana',
    'c' => 'cherry',
    'd' => 'date',
    'e' => 'elderberry',
    'f' => 'fig',
];

// use array_chunk() Block the array
$chunks = array_chunk($data, 2, true);

// Output the result after chunking
echo "Array after chunking:\n";
print_r($chunks);

// Suppose we need to recombinate each chunk
$combinedChunks = [];
foreach ($chunks as $chunk) {
    // Take the keys and values ​​of the chunked as arrays respectively,use array_combine() Recombination
    $keys = array_keys($chunk);
    $values = array_values($chunk);
    $combinedChunks[] = array_combine($keys, $values);
}

// 输出Recombination后的数组
echo "Recombination后的数组:\n";
print_r($combinedChunks);

// In the exampleURLReplacement operation
$url = "http://example.com";
$newUrl = str_replace("example.com", "m66.net", $url);
echo "ReplacedURL: " . $newUrl;
?>

Output result:

 Array after chunking:
Array
(
    [0] => Array
        (
            [a] => apple
            [b] => banana
        )

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

    [2] => Array
        (
            [e] => elderberry
            [f] => fig
        )
)

Recombination后的数组:
Array
(
    [0] => Array
        (
            [a] => apple
            [b] => banana
        )

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

    [2] => Array
        (
            [e] => elderberry
            [f] => fig
        )
)

ReplacedURL: http://m66.net

4. Explain the code

  1. Blocking : First, we use array_chunk() to divide the $data array into multiple small arrays according to the size of 2 elements per block. The parameter true means that the key that retains the original array.

  2. Recombination : Then, we obtain the keys and values ​​of each small array through array_keys() and array_values() respectively, and then use array_combine() to combine them into a new associative array.

  3. URL Replacement : The example also demonstrates how to replace the domain name section in the URL. Through the str_replace() function, replace the URL's domain name from example.com to m66.net .

Summarize

The combination of array_chunk() and array_combine() functions is very useful when processing data. array_chunk() can help you split a large array into small chunks, while array_combine() can help you recompose arrays as needed. The combination of these two can not only improve the readability of the code, but also effectively improve the efficiency of array processing.

Hopefully this example helps you better understand how to use these two PHP functions. If you have any other questions or need further help, feel free to ask!