如何将1D数组从数字转换为2D数组?

问题描述:

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));

我有一个数字来自这样的数字: p>

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

我试图转换 它使用两个for循环到2D,但我失败了。 尝试使用foreach循环是否更好,或者可以使用for-s完成? p>

我想做这样的事情: p>

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

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);