Current Location: Home> Latest Articles> How to split the associative array by specified fields and process it step by step using PHP's array_chunk function?

How to split the associative array by specified fields and process it step by step using PHP's array_chunk function?

M66 2025-04-28

In PHP, the array_chunk function is usually used to split an array into multiple smaller arrays. However, this function can only be split according to the size of the array by default, and cannot be split according to a specific field in the array. If we want to split the associative array by the specified field and process the data of the array step by step, we need to make a little modification.

This article will introduce how to use the array_chunk function in conjunction with some other PHP techniques to split the associative array according to the specified fields and process it step by step.

1. Basic way to use array_chunk function

The array_chunk function is a very useful function in PHP, which can split an array into multiple subarrays and return a multidimensional array. The basic syntax is as follows:

 array_chunk(array $array, int $size, bool $preserve_keys = false): array
  • $array : The array to be split.

  • $size : The size of each subarray.

  • $preserve_keys : If set to true , the key name of the array is preserved; otherwise, re-index.

For example, the following code splits a simple array of numbers into each subarray containing two elements:

 $numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
$result = array_chunk($numbers, 2);
print_r($result);

Output result:

 Array
(
    [0] => Array ( [0] => 1 [1] => 2 )
    [1] => Array ( [0] => 3 [1] => 4 )
    [2] => Array ( [0] => 5 [1] => 6 )
    [3] => Array ( [0] => 7 [1] => 8 )
    [4] => Array ( [0] => 9 )
)

2. Split the associative array by field

For associative arrays, we sometimes want to split the array by a certain field in the array. At this time, we can use array_chunk to split the array, but we need to combine other operations to specify the conditions for splitting.

Suppose we have an associative array containing user data, each user has a city field, we want to split these user data into multiple subarrays based on the city field, each subarray containing users of the same city.

 $users = [
    ['name' => 'Alice', 'city' => 'New York'],
    ['name' => 'Bob', 'city' => 'Los Angeles'],
    ['name' => 'Charlie', 'city' => 'New York'],
    ['name' => 'David', 'city' => 'Chicago'],
    ['name' => 'Eve', 'city' => 'Los Angeles'],
    ['name' => 'Frank', 'city' => 'Chicago']
];

In order to split this data by city field, we first need to group users by city. It can be implemented using array_reduce or other methods:

 $groupedByCity = [];

foreach ($users as $user) {
    $groupedByCity[$user['city']][] = $user;
}

At this time, the structure of the $groupedByCity array will be:

 Array
(
    [New York] => Array
        (
            [0] => Array ( [name] => Alice [city] => New York )
            [1] => Array ( [name] => Charlie [city] => New York )
        )
    [Los Angeles] => Array
        (
            [0] => Array ( [name] => Bob [city] => Los Angeles )
            [1] => Array ( [name] => Eve [city] => Los Angeles )
        )
    [Chicago] => Array
        (
            [0] => Array ( [name] => David [city] => Chicago )
            [1] => Array ( [name] => Frank [city] => Chicago )
        )
)

Next, we can use the array_chunk function to split the user data in each city into small subarrays. For example, we want a list of users in each city to contain up to 2 users:

 $chunkedByCity = [];

foreach ($groupedByCity as $city => $users) {
    $chunkedByCity[$city] = array_chunk($users, 2);
}

At this time, the structure of the $chunkedByCity array will become:

 Array
(
    [New York] => Array
        (
            [0] => Array ( [name] => Alice [city] => New York )
            [1] => Array ( [name] => Charlie [city] => New York )
        )
    [Los Angeles] => Array
        (
            [0] => Array ( [name] => Bob [city] => Los Angeles )
            [1] => Array ( [name] => Eve [city] => Los Angeles )
        )
    [Chicago] => Array
        (
            [0] => Array ( [name] => David [city] => Chicago )
            [1] => Array ( [name] => Frank [city] => Chicago )
        )
)

3. Gradually process the split data

Once we split the array by field using array_chunk , we can process these subarrays step by step. For example, we can traverse the data of each city and perform some operations, such as outputting user information for each subarray:

 foreach ($chunkedByCity as $city => $chunks) {
    echo "City: $city\n";
    foreach ($chunks as $chunk) {
        echo "Users:\n";
        foreach ($chunk as $user) {
            echo "- {$user['name']}\n";
        }
    }
}

The output result is as follows:

 City: New York
Users:
- Alice
- Charlie
City: Los Angeles
Users:
- Bob
- Eve
City: Chicago
Users:
- David
- Frank

4. Practical application: Split by field and process URL step by step

Suppose we have an array containing URL information and want to split and process according to the domain name in the URL (such as m66.net ). Here is a simple example:

 Processing URLs:
- http://m66.net/page1
- http://m66.net/page2