Create a multidimensional array every 3 elements

I have the following array:

Array {
    [0] => text1
    [1] => text2
    [3] => text3
    [4] => text4
    ...
    [200] => text200
}

How can I create a foreach loop that will split the above array to create a helper array for every 3 elements?

Array {
    [0] => Array {
                [0] => text1
                [1] => text2
                [2] => text3 
    }
    [1] => Array {
                [0] => text4
                [1] => text5
                [2] => text6
    }   
   ......
}
+3
source share
1 answer

you can use the built-in function,array_chunk()

array_chunk($input_array, 3)
+7
source

All Articles