Current Location: Home> Latest Articles> Use array_chunk to convert data using array_chunk in combination with array_map function

Use array_chunk to convert data using array_chunk in combination with array_map function

M66 2025-04-26

In PHP, array_map and array_chunk are two very useful functions, which are used to callback function processing and split arrays into small chunks, respectively. Combining these two functions, we can efficiently convert and process data. Today we will use a practical example to show how these two functions can be used in combination to implement more complex data conversion tasks.

1. Basic concepts of array_map and array_chunk

  • array_map : This function takes a callback function and applies it to each element of the array, returning a new array.

    Example:

     $arr = [1, 2, 3, 4];
    $result = array_map(function($item) {
        return $item * 2;
    }, $arr);
    print_r($result); // Output: [2, 4, 6, 8]
    
  • array_chunk : Split an array into multiple small arrays of specified size. Each small array contains the same number of elements, and the last small array may contain fewer elements than other arrays.

    Example:

     $arr = [1, 2, 3, 4, 5, 6, 7, 8];
    $chunks = array_chunk($arr, 3);
    print_r($chunks); // Output: [[1, 2, 3], [4, 5, 6], [7, 8]]
    

2. Use array_map and array_chunk for data conversion

We can combine array_map and array_chunk to handle more complex situations. For example, suppose we have an array containing user data, each user has an id and name , we want:

  1. Split these users by size of each group.

  2. Processing users within each group, for example converting each user's name into capital letters.

Code example:

 <?php

// Simulate an array containing user data
$users = [
    ['id' => 1, 'name' => 'Alice'],
    ['id' => 2, 'name' => 'Bob'],
    ['id' => 3, 'name' => 'Charlie'],
    ['id' => 4, 'name' => 'David'],
    ['id' => 5, 'name' => 'Eve']
];

// use array_chunk Put user data on each2Divide into a small group
$userChunks = array_chunk($users, 2);

// use array_map Process users in each group,Will name Convert to capital
$processedChunks = array_map(function($chunk) {
    return array_map(function($user) {
        $user['name'] = strtoupper($user['name']); // Will用户名字转成大写
        return $user;
    }, $chunk);
}, $userChunks);

// Output处理后的结果
print_r($processedChunks);

?>

Output result:

 Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [name] => ALICE
                )

            [1] => Array
                (
                    [id] => 2
                    [name] => BOB
                )
        )

    [1] => Array
        (
            [0] => Array
                (
                    [id] => 3
                    [name] => CHARLIE
                )

            [1] => Array
                (
                    [id] => 4
                    [name] => DAVID
                )
        )

    [2] => Array
        (
            [0] => Array
                (
                    [id] => 5
                    [name] => EVE
                )
        )
)

3. Application scenarios

By combining array_map and array_chunk , we can process data more flexiblely. For example, this combination method can be used to display user data on paging, batch update user information, or batch operations. In large-scale data processing tasks, this method is both efficient and convenient.

4. Summary

  • array_map is used to iterate over the array and apply a callback function to each element.

  • array_chunk is used to split an array into multiple small arrays.

  • Combining the two allows us to process data more flexibly.

Hopefully, with this simple example, you can understand how to use these two functions in combination to meet various needs in actual development.