如何在具有固定数量的项目的情况下分离一个收藏?



我有这样的东西:

$test = collect([
123,
234,
345,
456,
...
]);

,包含 1000 个项目。我想将其分离为子集合。我的意思是,我希望看到以下结果:

[
    [
    // 500 items here 
    ],
    [
    // and 500 items here
    ]
]

如果我有 2000 个项目,那么我希望看到:

[
    [
    // 500 items here 
    ],
    [
    // and 500 items here
    ],
    [
    // 500 items here 
    ],
    [
    // and 500 items here
    ]
]

等等。哪种方法是正确的方法?

尝试

 $res = $test->chunk(500);

在视图中,您可以使用foreach循环来获取结果

$collection = collect([1, 2, 3, 4, 5, 6, 7, 8]);
$chunks = $collection->chunk(4);
$chunks->toArray();

这应该输出:

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

最新更新