Current Location: Home> Latest Articles> The array key names returned by array_chunk are continuous, is it OK?

The array key names returned by array_chunk are continuous, is it OK?

M66 2025-04-25

In PHP, array_chunk is a very practical function that can split a large array into multiple small arrays, and it also allows us to define the maximum length of each small array.

The function signature is as follows:

 array_chunk(array $array, int $length, bool $preserve_keys = false): array
  • $array : The input array.

  • $length : The length of each split array.

  • $preserve_keys : If set to true , the returned array retains the key name of the original array; if false , the returned array regenerates consecutive numeric key names.

1. Will the new array key name returned after array_chunk remain unchanged?

By default, array_chunk will determine whether to retain the key name of the original array based on the third parameter $preserve_keys :

  • When $preserve_keys is set to false (the default) , the returned new array will use consecutive numeric key names, starting from 0 .

  • When $preserve_keys is set to true , the new array retains the key name of the original array.

Example: $preserve_keys is false

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

$result = array_chunk($array, 2);

print_r($result);
?>

Output result:

 Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
        )
    [1] => Array
        (
            [0] => c
            [1] => d
        )
    [2] => Array
        (
            [0] => e
        )
)

As you can see, in the above code, $preserve_keys defaults to false , so the key names of the returned array are renumbered to 0, 1, 2 , and do not retain the key names such as 10, 20, 30 in the original array.

Example: $preserve_keys is true

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

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

print_r($result);
?>

Output result:

 Array
(
    [10] => Array
        (
            [0] => a
            [1] => b
        )
    [20] => Array
        (
            [0] => c
            [1] => d
        )
    [30] => Array
        (
            [0] => e
        )
)

In this example, $preserve_keys is set to true , so the returned array retains the key names in the original array: 10, 20, 30 .

2. Are the key names of the new array returned by array_chunk continuous?

  • When $preserve_keys is set to false , the key names of the returned new array are continuous. That is, the new array will start at 0 and be numbered with consecutive numbers.

  • When $preserve_keys is set to true , the returned new array retains the original key name, so the key name of the new array may not be continuous, but is consistent with the key name of the corresponding split part of the original array.

3. Summary

The behavior of array_chunk depends on the $preserve_keys parameter:

  • By default ( $preserve_keys = false ), the key names of the new array are continuous, starting from 0 .

  • If $preserve_keys = true is set, the new array retains the key name of the original array.

If you need a continuous numeric key name, you should set $preserve_keys to false otherwise you can preserve the original key name.