Current Location: Home> Latest Articles> How to ensure that the key names of chunked are not lost when splitting arrays using array_chunk

How to ensure that the key names of chunked are not lost when splitting arrays using array_chunk

M66 2025-04-26

In PHP, array_chunk() is a very common array operation function. It can divide a large array into multiple small arrays and return a two-dimensional array containing the split result. Usually, array_chunk() will reset the key name of the array, causing the key of each subarray to start from 0 and lose the key name of the original array. What if you want to keep the original key name when splitting an array?

This article will explain how to keep the original key name from resetting or missing when splitting an array using array_chunk() .

1. array_chunk() default behavior

First, let’s look at the default behavior of array_chunk() . Suppose we have an array with several elements and these elements have custom key names:

 <?php
$array = [
    1 => 'apple',
    2 => 'banana',
    3 => 'cherry',
    4 => 'date',
    5 => 'elderberry'
];

$chunkedArray = array_chunk($array, 2);
print_r($chunkedArray);
?>

The output result will look like this:

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

As you can see, array_chunk() splits the original array into multiple small arrays, but all small array key names are reset to start from 0 , losing the original key.

2. How to keep the original key name?

In order to keep the key names of the original array not lost, you can use the third parameter preserve_keys . By default, this parameter is false , which is why the original key is lost. If set to true , array_chunk() retains the original key name.

 <?php
$array = [
    1 => 'apple',
    2 => 'banana',
    3 => 'cherry',
    4 => 'date',
    5 => 'elderberry'
];

$chunkedArray = array_chunk($array, 2, true);
print_r($chunkedArray);
?>

The output result will be:

 Array
(
    [0] => Array
        (
            [1] => apple
            [2] => banana
        )
    [1] => Array
        (
            [3] => cherry
            [4] => date
        )
    [2] => Array
        (
            [5] => elderberry
        )
)

As you can see, now each small array retains the key name of the original array, and the array key starts with the key of the original array and will not be reset.

3. Summary

  • array_chunk() will lose the key name of the original array by default, because it will reset the key to start from 0 .

  • If you need to preserve the key name of the original array, just set the third parameter of array_chunk() to true .

  • When using the preserve_keys parameter, the key name of each small array returned will be consistent with the original array.

I hope that through this article, you can understand more clearly how to retain the original key name when using array_chunk() . This allows for more flexibility in handling split arrays, especially when key name information is required.