Categories
PHP Tips & Tutorials

PHP: Divide and Split an Array into Chunks (Sections) or Several Sub-arrays

A big array is awkward. To divide the big array and split it into smaller sub-arrays or children arrays by size, you can use the PHP function array_chunk():

$sections = array_chunk(array('k', 'l', 'm', 'n', 'o'), 2);

To slice $sections into smaller arrays by the size of 2, and $sections will look like this:

Array
(
    [0] => Array
        (
            [0] => k
            [1] => l
        )

    [1] => Array
        (
            [0] => m
            [1] => n
        )

    [2] => Array
        (
            [0] => o
        )

)

By Yang Yang

Hello, I'm Yang. I build online businesses that please people. Want to join in and post some useful articles on Kavoir.com? Shoot me a message.