Current Location: Home> Latest Articles> Is array_chunk applicable when processing associative arrays?

Is array_chunk applicable when processing associative arrays?

M66 2025-04-25

In PHP, the array_chunk function is used to split an array into multiple smaller arrays. Usually, we use it to split a numeric index array. However, when we deal with associative arrays, many developers have questions: Will the array_chunk function retain the key name? The answer to this question is: not completely reserved .

Introduction to array_chunk function

First, let’s quickly review how the array_chunk function is used. The basic usage of array_chunk is:

 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 subarray.

  • $preserve_keys : Whether to preserve the key name of the original array, default to false .

If $preserve_keys is set to true , array_chunk will retain the key names in the original array. If set to false (default is false ), the key name is re-indexed.

The behavior of key names when dealing with associative arrays

Let's use some concrete examples to verify whether the key name will be preserved when array_chunk is used to associate arrays.

Example 1: No key names are retained

 $array = [
    'a' => 'apple',
    'b' => 'banana',
    'c' => 'cherry',
    'd' => 'date'
];

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

The output result is:

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

You can see that the key name has been lost, and the key in the result has been re-indexed from 0 .

Example 2: Reserved key names

 $array = [
    'a' => 'apple',
    'b' => 'banana',
    'c' => 'cherry',
    'd' => 'date'
];

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

The output result is:

 Array
(
    [0] => Array
        (
            [a] => apple
            [b] => banana
        )
    [1] => Array
        (
            [c] => cherry
            [d] => date
        )
)

In this example, the key name is preserved and array_chunk returns each subarray with the original key name.

Is it suitable to split an associative array?

array_chunk is useful for splitting the associative array, but be aware of its behavior: if you want to preserve the original key name, you need to set the third parameter $preserve_keys to true . Otherwise, the key names will be reindexed, which may not be suitable for some scenarios where you need to retain the key names.

Also, if you don't need to keep the key names and only focus on splitting the array content, the default behavior is enough.

Summarize

  • array_chunk does not retain key names by default and will re-index the keys of subarrays.

  • If you need to preserve the key name, you can do it by setting the $preserve_keys parameter to true .

  • When splitting the associative array, array_chunk is fully applicable, but it is necessary to decide whether to retain the key name according to your needs.

Example of URL replacement involved in the code

Suppose you use a URL in your code, like this:

 $url = 'https://m66.net/page';
$parsed_url = parse_url($url);
print_r($parsed_url);