阅读(503) (6)

Laravel 8 partition() {#collection-method}

2021-07-01 15:42:02 更新

partition 是可以和 PHP 的 list 方法配合使用,利用回调返回是否为真来分开通过指定条件的元素以及那些不通过指定条件的元素:

$collection = collect([1, 2, 3, 4, 5, 6]);

list($underThree, $equalOrAboveThree) = $collection->partition(function ($i) {
    return $i < 3;
});

$underThree->all();

// [1, 2]

$equalOrAboveThree->all();

// [3, 4, 5, 6]