YouNoobsDie
YouNoobsDie

Reputation: 39

How can I convert 1D array from numbers to 2D array?

I have an array from numbers like this :

$array = array (1,2,3,4,5,6,7,8,9,10,11,12);

I tried to convert it to 2D using two for loops but i failed. Is it better to try with foreach loop , or it can be done with for-s ?

I wanna make something like this:

$array = array (array (1,2,3,4), array(5,6,7,8), array(9,10,11,12));

Upvotes: 0

Views: 489

Answers (1)

AbraCadaver
AbraCadaver

Reputation: 78994

The reason I'm posting an answer is that a question like this and the availability of array_chunk() may not be very intuitive:

$array = array_chunk($array, 4);

Upvotes: 3

Related Questions