dongzongxun8491
2015-03-31 18:41
浏览 118
已采纳

如何将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));

图片转代码服务由CSDN问答提供 功能建议

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

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

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

我想做这样的事情:

  $ array = array(array(1,2,3,4),array(5,6,7,8),array(9,10,11,12)); 
    
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doutinghou6980 2015-03-31 18:42
    已采纳

    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);
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题