Current Location: Home> Latest Articles> Use array_chunk and array_map to batch modify the array content

Use array_chunk and array_map to batch modify the array content

M66 2025-04-28

In PHP, efficiency issues often become performance bottlenecks when dealing with large arrays. When we batch modify the array content, if we can reasonably split the array and perform parallel processing, we can effectively improve the execution efficiency of the code. array_chunk and array_map are two powerful PHP array operation functions. Combining these two functions can greatly improve the performance when batch processing of large arrays.

1. Introduction to array_chunk and array_map

  • array_chunk : This function is used to split a large array into multiple smaller arrays for easy segmentation processing. It is used as follows:

     array_chunk($array, $size);
    

    Here, $array is the original array to be divided, and $size is the size of each subarray.

  • array_map : This function is used to apply a callback function to each element in the array, thus returning a modified array. The basic syntax is as follows:

     array_map($callback, $array);
    

    Where $callback is a callback function used to process each array element, and $array is an array to be processed.

2. Why combine array_chunk and array_map ?

When dealing with large arrays, directly processing the entire array can take up a lot of memory, especially if each element needs to be modified. If we can split the array into multiple smaller subarrays, processing one subarray at a time, it can not only reduce memory usage, but also improve the execution efficiency of the code.

Using array_chunk and array_map in combination, we can first split the large array into smaller subarrays, then batch modify the data in each subarray through array_map , and finally combine the modified subarrays into the final result.

3. Example: Batch modify array content

Suppose we have an array containing user information, we need to batch update each user's email address. Specifically, we modify the email domain name from example.com to m66.net .

 <?php

// Original user information array
$users = [
    ['id' => 1, 'name' => 'Alice', 'email' => 'alice@example.com'],
    ['id' => 2, 'name' => 'Bob', 'email' => 'bob@example.com'],
    ['id' => 3, 'name' => 'Charlie', 'email' => 'charlie@example.com'],
    ['id' => 4, 'name' => 'David', 'email' => 'david@example.com'],
    // Other users...
];

// Each processing 2 A user
$chunkedUsers = array_chunk($users, 2);

// use array_map Batch modify user data in each subarray
$updatedUsers = array_map(function($chunk) {
    return array_map(function($user) {
        $user['email'] = str_replace('@example.com', '@m66.net', $user['email']);
        return $user;
    }, $chunk);
}, $chunkedUsers);

// Combine the modified sub-regions back to a large array
$updatedUsers = array_merge(...$updatedUsers);

// Output modified user data
print_r($updatedUsers);

?>

4. Code Analysis

  1. Split array : First, use array_chunk to split the original array $users into smaller arrays containing 2 users per subarray. The advantage of this is that even if there are thousands of users, they can be processed in batches.

  2. Batch Modification : In each subarray, use array_map to modify the user's email address. Through the callback function, we use str_replace to replace the domain name @example.com in the mailbox with @m66.net .

  3. Merge result : After the modification is completed, we use array_merge to re-merge all subarrays into an array to get the final update result.

5. Optimize code efficiency

By splitting the array into multiple smaller parts, we can more effectively utilize PHP's memory management to avoid the performance problems caused by loading large amounts of data at once. At the same time, array_map is a highly optimized function that can operate on each element efficiently. Combining these two can not only reduce memory usage, but also improve processing speed.

6. Summary

Using a combination of array_chunk and array_map to batch modify the contents in PHP arrays can significantly improve the efficiency of handling large-scale arrays. By reasonably splitting arrays and optimizing batch operations, we can better manage memory and speed up data processing. This method is especially effective for application scenarios that require processing large amounts of data.