Current Location: Home> Latest Articles> How to use array_chunk and array_walk to implement batch updates?

How to use array_chunk and array_walk to implement batch updates?

M66 2025-04-26

In PHP, array_chunk and array_walk are two very commonly used array operation functions. They are used to cut the array into smaller chunks, and to execute callback functions on each element in the array. Today, we will discuss how to use these two functions in combination to implement batch update operations on arrays.

1. Basic introduction to array_chunk and array_walk

array_chunk

The array_chunk function divides a large array into multiple small array chunks. Its basic syntax is as follows:

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

  • $size : The size of each small piece.

  • $preserve_keys : Whether to preserve the keys of the original array.

array_chunk returns a multi-dimensional array, with each subarray having a maximum size of the specified $size .

array_walk

The array_walk function is used to iterate over the array and apply the specified callback function to each element. Its basic syntax is as follows:

 array_walk(array &$array, callable $callback): bool
  • $array : The array to be operated, passed by reference.

  • $callback : A callback function used to process each array element.

array_walk applies a callback function to each element of the array, and the callback function receives the value and key of the current array element as parameters.

2. Combining array_chunk and array_walk to achieve batch updates

Suppose we have an array with a lot of data, and we want to batch update the array (e.g. modify some values ​​in the array, or add some new information to the array). To avoid single processing of the entire array, we can split the array into multiple small pieces through array_chunk , and then perform update operations through array_walk .

Sample code

Here is an example of using array_chunk and array_walk to implement batch updates. Suppose we want to process an array with multiple URL addresses and we need to change the domain name in all URLs to m66.net .

 <?php

// Original array,Contains multiple URL
$urls = [
    'http://www.example.com/path1',
    'https://www.example.com/path2',
    'http://www.example.com/path3',
    'https://www.example.com/path4',
];

// use array_chunk 将Original array分成每块最多 2 Elements
$chunks = array_chunk($urls, 2);

// 遍历每个小块并use array_walk 来修改每Elements
foreach ($chunks as &$chunk) {
    // use array_walk Modify elements in each block
    array_walk($chunk, function(&$url) {
        // Replace the domain name as m66.net
        $url = preg_replace('/https?:\/\/[^\/]+/', 'https://m66.net', $url);
    });
}

// Output updated results
foreach ($chunks as $chunk) {
    print_r($chunk);
}

?>

Code description:

  1. We first define an array $urls containing multiple URLs.

  2. Then use array_chunk to divide the array into small pieces of size 2. In this way, $chunks will become a two-dimensional array, each subarray contains up to 2 elements.

  3. Next, we use array_walk to iterate through each subarray (i.e., each small block) and replace the domain name part of each URL with m66.net via a regular expression.

  4. Finally, we output the modified array.

3. Why use array_chunk and array_walk ?

advantage:

  • Performance optimization : array_chunk divides large arrays into small chunks, which can reduce memory consumption and computational volume per operation, especially when dealing with very large data sets.

  • Flexibility : Use array_walk to easily customize data in each chunk, such as modifying certain fields, replacing strings, etc.

Use scenarios:

  • Batch data processing, such as updates of database records, batch API requests, etc.

  • Data cleaning, string replacement and other operations.

4. Summary

By combining array_chunk and array_walk , we can easily perform batch update operations on large arrays. First, split the array into smaller chunks through array_chunk , and then use array_walk to update each element one by one. This approach not only simplifies code logic, but also improves memory and performance efficiency.

Hopefully this article helps you understand how to use these two functions in PHP to handle batch update tasks of arrays. If you have any questions or further needs, please feel free to communicate!