How to use array_chunk function in PHP to split an array into chunks of specified size

王林
Release: 2023-06-26 12:18:01
Original
840 people have browsed it

In PHP, it is often necessary to operate on arrays. One of the common operations is to split the array into blocks of a specified size. At this time, we can use the PHP built-in function array_chunk() to achieve this purpose.

The syntax of the array_chunk() function is:

array array_chunk ( array $array , int $size [, bool $preserve_keys = FALSE ] )
Copy after login

Among them, the $array parameter represents the array that needs to be divided, and the $size parameter represents the number of elements contained in each divided block. The $preserve_keys parameter indicates whether to retain the key names of the original array. The default is false, which means not to retain.

Below, we use an example to demonstrate how to use the array_chunk() function to split an array into chunks of a specified size.

Suppose we have an array of 10 elements and we want to split it into chunks of size 3. The code is as follows:

// 创建一个包含10个元素的数组
$arr = array(1,2,3,4,5,6,7,8,9,10);

// 使用array_chunk()函数将数组分割成大小为3的块
$chunks = array_chunk($arr, 3);

// 输出分割后的块
print_r($chunks);
Copy after login

The output result is as follows:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

    [1] => Array
        (
            [0] => 4
            [1] => 5
            [2] => 6
        )

    [2] => Array
        (
            [0] => 7
            [1] => 8
            [2] => 9
        )

    [3] => Array
        (
            [0] => 10
        )

)
Copy after login

It can be seen from the output result that the original array was successfully divided into blocks of size 3, and the last block only contains one element .

In addition to the default situation in the above example, we can also retain the key names of the original array by setting the $preserve_keys parameter to true. The code is as follows:

// 创建一个包含10个元素的关联数组
$arr = array(
    'a' => 1,
    'b' => 2,
    'c' => 3,
    'd' => 4,
    'e' => 5,
    'f' => 6,
    'g' => 7,
    'h' => 8,
    'i' => 9,
    'j' => 10
);

// 使用array_chunk()函数将关联数组分割成大小为3的块,并保留键名
$chunks = array_chunk($arr, 3, true);

// 输出分割后的块
print_r($chunks);
Copy after login

The output result is as follows:

Array
(
    [0] => Array
        (
            [a] => 1
            [b] => 2
            [c] => 3
        )

    [1] => Array
        (
            [d] => 4
            [e] => 5
            [f] => 6
        )

    [2] => Array
        (
            [g] => 7
            [h] => 8
            [i] => 9
        )

    [3] => Array
        (
            [j] => 10
        )

)
Copy after login

It can be seen from the output result that the key names of the original array are successfully retained, and the divided array blocks also contain the corresponding key names. .

Through the demonstration of the above example, we can see that it is very simple to use the array_chunk() function in PHP to split an array into chunks of a specified size. You only need to pass the array to be split and the number of each chunk. Just size.

The above is the detailed content of How to use array_chunk function in PHP to split an array into chunks of specified size. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!