Or Weinberger
Or Weinberger

Reputation: 7472

Create a multi dimensional 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 divide the above array to create a sub array for every 3 elements?

Array {
    [0] => Array {
                [0] => text1
                [1] => text2
                [2] => text3 
    }
    [1] => Array {
                [0] => text4
                [1] => text5
                [2] => text6
    }   
   ......
}

Upvotes: 3

Views: 191

Answers (1)

Haim Evgi
Haim Evgi

Reputation: 125476

you can use a build in function , array_chunk()

array_chunk($input_array, 3)

Upvotes: 7

Related Questions