假设您有以下数组:
[1, 2, 3, 4, 5, 6, 7]
我如何在php中拆分它以便我拥有:
[[1, 2, 3], [4, 5, 6] [7]] // divide by three.
我知道它会像array % 3
,但我确实不确定......
想法?
答案 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));
?>