Current Location: Home> Latest Articles> Detailed explanation of the preserve_keys parameter of array_chunk

Detailed explanation of the preserve_keys parameter of array_chunk

M66 2025-04-28

What exactly does the preserve_keys parameter of the array_chunk function in PHP do? How to use it to preserve array key values?

In PHP, the array_chunk function is a commonly used function that is used to split a large array into multiple small arrays. The basic syntax of this function is as follows:

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

Parameter analysis

  • $array : The array to be divided.

  • $size : The size of each subarray.

  • $preserve_keys : A boolean value indicating whether the key value of the original array is retained.

Here, the most commonly used parameters are $size and $preserve_keys . This article will focus on explaining the function of the $preserve_keys parameter and how to use it correctly.

The function of array_chunk function

The array_chunk function can divide an array into multiple small arrays, and the size of each small array is determined by $size . By default, the split array will use a new key value and will be renumbered from 0. If you need to preserve the key value of the original array, you can use the preserve_keys parameter to achieve it.

The function of the $preserve_keys parameter

$preserve_keys is a Boolean parameter. By default, its value is false , which means that the split subarray will lose the key value of the original array and start again from 0. If $preserve_keys is set to true , the key value of the original array will be retained.

Example 1: No key value reserved

 $array = [
    10 => 'a',
    20 => 'b',
    30 => 'c',
    40 => 'd',
    50 => 'e',
];

$result = array_chunk($array, 2);  // Key values ​​are not retained by default

print_r($result);

Output:

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

    [1] => Array
        (
            [0] => c
            [1] => d
        )

    [2] => Array
        (
            [0] => e
        )
)

As you can see, the split array reassigns key values ​​starting from 0 to each subarray.

Example 2: Reserve key values

 $array = [
    10 => 'a',
    20 => 'b',
    30 => 'c',
    40 => 'd',
    50 => 'e',
];

$result = array_chunk($array, 2, true);  // set uppreserve_keysfortrue,Reserve key values

print_r($result);

Output:

 Array
(
    [0] => Array
        (
            [10] => a
            [20] => b
        )

    [1] => Array
        (
            [30] => c
            [40] => d
        )

    [2] => Array
        (
            [50] => e
        )
)

At this time, the divided subarray retains the key value of the original array.

How to use preserve_keys to preserve array key values

When using the preserve_keys parameter, you only need to set it to true when calling array_chunk to preserve the key value of the original array. For example, suppose you have an array where key values ​​make sense and you want to keep those key values, you can do this:

 $array = [
    0 => 'apple',
    1 => 'banana',
    2 => 'cherry',
    3 => 'date',
];

$chunks = array_chunk($array, 2, true);

foreach ($chunks as $chunk) {
    foreach ($chunk as $key => $value) {
        echo "Key: $key, Value: $value\n";
    }
}

Output:

 Key: 0, Value: apple
Key: 1, Value: banana
Key: 2, Value: cherry
Key: 3, Value: date

As you can see, the split array still retains the key value of the original array.

Summarize

Through the preserve_keys parameter of the array_chunk function, we can easily control whether to retain the key value of the original array. By default, key values ​​are lost and renumbered from 0; but if you want to keep the original key values, just set preserve_keys to true .

This is very useful for scenarios where key values ​​need to be retained, such as maintaining the structure and key-value relationship of the original array when splitting some index values ​​or associating arrays.

Notice: