laravel将多个数组的集合合并成一个数组的集合/拆分成多个数组
程序员文章站
2022-07-15 11:58:35
...
//chunk()集合拆分
$collection = collect([1, 2, 3, 4, 5, 6, 7]);//数组转为集合
$chunks = $collection->chunk(4);//chunk是拆分每一个数组的个数
$chunks->toArray(); //结果转成数组
// [[1, 2, 3, 4], [5, 6, 7]]
//collapse()集合合并
$collection = collect([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
$collapsed = $collection->collapse();
$collapsed->all();
// [1, 2, 3, 4, 5, 6, 7, 8, 9]
//array_collapse()数组直接合并
$array = array_collapse([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
// [1, 2, 3, 4, 5, 6, 7, 8, 9]