douyangquan2474 2016-10-25 11:48
浏览 27
已采纳

将数组划分为3个相等列

I have a dynamic number of items in which I'll need to divide into 3 columns. Let's say I'm given this:

array("one", "InfoOne", "LibOne", 
      "two", "InfoTwo", "LibTwo", 
      "three", "InfoThree", "LibThree")

I need to generate array like this:

array( 
[0] = array("one", "InfoOne", "LibOne"),
[1] = array("two", "InfoTwo", "LibTwo"),
[2] = array("three", "InfoThre", "LibThree")
)

How could I do to put in an array, data with 3 equal columns?

  • 写回答

3条回答 默认 最新

  • douyan1882 2016-10-25 11:51
    关注

    Using array_chunk() you can do this-

    $ori = array("one", "InfoOne", "LibOne", 
          "two", "InfoTwo", "LibTwo", 
          "three", "InfoThree", "LibThree");
    $chunked = array_chunk($ori, 3);
    echo '<pre>';
    print_r($chunked);
    echo '</pre>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?