Current Location: Home> Latest Articles> How can we keep the original array index when splitting an array using array_chunk?

How can we keep the original array index when splitting an array using array_chunk?

M66 2025-04-26

In PHP, array_chunk() is a very common function that is used to split a large array into several small arrays and return it. By default, array_chunk() resets the index of each small array, which counts the indexes from 0. If you want to keep the original index when splitting the array, you can use an extra parameter to do so.

Basic usage

First, let's take a look at the basic usage of array_chunk() . Suppose we have an array containing multiple elements and use array_chunk() to split this array:

 <?php
$originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9];
$chunkedArray = array_chunk($originalArray, 3);

print_r($chunkedArray);
?>

After execution, the result we get is:

 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 you can see, the index of each small array is counted from 0, and the index of the original array is reset.

Keep the original array index

To keep the original array index, we just need to set the third parameter of array_chunk() to true . This parameter ensures that the split array still retains the original key name.

 <?php
$originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9];
$chunkedArray = array_chunk($originalArray, 3, true);

print_r($chunkedArray);
?>

After execution, the result we get is:

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

    [3] => Array
        (
            [3] => 4
            [4] => 5
            [5] => 6
        )

    [6] => Array
        (
            [6] => 7
            [7] => 8
            [8] => 9
        )
)

Now you can see that the index in each small array keeps the index of the original array.

Why keep the original index?

Keeping the original index can be very useful in some cases, especially if you need to do further operations based on the index. For example, if you want to find the original location of an element in a split array or process related logic, retaining the index will make your code more flexible.

Sample application scenarios

Suppose we have an array of users, each user has a unique ID, and if we split this array and keep the index, we can more easily track the original location of each user. for example:

 <?php
$users = [
    101 => 'Alice',
    102 => 'Bob',
    103 => 'Charlie',
    104 => 'David',
    105 => 'Eve',
    106 => 'Frank'
];

$chunkedUsers = array_chunk($users, 2, true);

print_r($chunkedUsers);
?>

Output result:

 Array
(
    [101] => Array
        (
            [101] => Alice
            [102] => Bob
        )

    [103] => Array
        (
            [103] => Charlie
            [104] => David
        )

    [105] => Array
        (
            [105] => Eve
            [106] => Frank
        )
)

By retaining the index, we can process each small array directly based on the original user ID.

Summarize

When splitting an array using array_chunk() , if you want to keep the original array index, just set the third parameter of the function to true . This ensures that the split array retains its original key value instead of starting from 0 again. This feature is very useful when dealing with arrays that need to preserve index information.