PHP array_chunk: Split Arrays Easily

In PHP, the function array_chunk is used to divide an array into multiple smaller arrays of equal size, which are then grouped together to form a new two-dimensional array and returned. The size of each smaller array is determined by the second parameter. If the original array cannot be evenly divided, the size of the last smaller array may be smaller than the specified size.

The syntax of the array_chunk function is as follows:
array_chunk(array, size, preserve_keys)

Description of Parameters:

  1. Array: The array to be divided.
  2. Size: The size of each individual array.
  3. preserve_keys: an optional parameter that, when set to true, will keep the original array keys. The default value is false.

Original: 我很高兴认识你。
Paraphrased: I am glad to meet you.

$array = array('a', 'b', 'c', 'd', 'e', 'f');
$chunks = array_chunk($array, 2); // 将数组分割成大小为2的小数组
print_r($chunks);

Output:

Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
        )

    [1] => Array
        (
            [0] => c
            [1] => d
        )

    [2] => Array
        (
            [0] => e
            [1] => f
        )

)
bannerAds