在php中将数组拆分为多个部分

时间:2015-12-27 19:35:53

标签: php arrays

假设您有以下数组:

[1, 2, 3, 4, 5, 6, 7]

我如何在php中拆分它以便我拥有:

[[1, 2, 3], [4, 5, 6] [7]] // divide by three.

我知道它会像array % 3,但我确实不确定......

想法?

1 个答案:

答案 0 :(得分:2)

尝试使用特殊的php函数 array_chunk()learn more

 <?php
      $cars = array("Volvo", "BMW", "Toyota", "Honda", "Mercedes", "Opel");
      // second argument defines how many chunks you gonna make
      print_r(array_chunk($cars, 3));
 ?>