Laravel 集合 Collection
1.简介
Illuminate\Support\Collection
类提供了一个更具可读性的、更便于处理数组数据的封装,具体例子看下面的代码。
我们使用了 collect
函数从数组中创建新的集合实例,对其中的每个元素运行 strtoupper
函数之后再移除所有的空元素
$collection = collect(['pinux', 'php', null])->map(function ($name) { return strtoupper($name); }) ->reject(function ($name) { return empty($name); });
上面的列子可以看出,Collection
类允许你链式调用其方法,以达到在底层数组上优雅地执行 map 和 reject 操作。一般来说,集合是不可改变的,这意味着每个 Collection
方法都会返回一个全新的 Collection
实例。
2.创建集合
辅助函数 collect
会为给定的数组返回一个新的 Illuminate\Support\Collection
实例。也就是说,创建一个集合就这么简单:
$collection = collect(['php', 'redis', 'laravel']);
默认情况下, EIoquent查询的结果返回的内容都是 Collection
实例。
3.集合可用的方法
以下是Collection
类每个可用的方法。**记住,所有方法都可以以方法链的形式优雅地操纵数组。**而且,几乎所有的方法都会返回新的 Collection
实例,允许你在必要时保存集合的原始副本。
#1.all方法,该方法返回该集合表示的底层数组。 collect([1, 2, 3])->all(); // [1, 2, 3] #2.avg方法,返回给定键的平均值。 #获取数组的平均值 $average = collect([1, 1, 2, 4])->avg(); // 2 #获取二维数组的平均值 $average = collect([['foo' => 10], ['foo' => 10], ['foo' => 20], ['foo' => 40]])->avg('foo'); // 20 #3.chuck方法,将集合拆分多个指定大小的小集合。 $collection = collect([1, 2, 3, 4, 5, 6, 7]); $chunks = $collection->chunk(4); $chunks->toArray(); // [[1, 2, 3, 4], [5, 6, 7]] #这个方法比较适用在使用网格系统时的视图中,如 Bootstrap。 #想象一下有一个 Eloquent 模型的集合要在网格中显示 @foreach ($products->chunk(3) as $chunk) <div class="row"> @foreach ($chunk as $product) <div class="col-xs-4">{{ $product->name }}</div> @endforeach </div> @endforeach #4.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] #5.combine方法,将一个集合的值作为「键」,再将另一个数组或者集合#的值作为「值」合并成一个集合。 $collection = collect(['name', 'age']); $combined = $collection->combine(['George', 29]); $combined->all(); // ['name' => 'George', 'age' => 29] #6.判断集合是否包含给定的项目。 #基本用法 $collection = collect(['name' => 'Desk', 'price' => 100]); $collection->contains('Desk'); // true $collection->contains('New York'); // false #也可以用 contains 方法匹配一对键/值,即判断给定的配对是否存在于集合中 $collection = collect([ ['product' => 'Desk', 'price' => 200], ['product' => 'Chair', 'price' => 100], ]); $collection->contains('product', 'Bookcase'); // false dd($collection->contains('price', '100')); // true #也可以传递一个回调到 contains 方法来执行自己的真实测试 $collection = collect([1, 2, 3, 4, 5]); $collection->contains(function ($value, $key) { return $value > 5; }); // false #contains 方法在检查项目值时使用「宽松」比较,意味着具有整数值的字符串将被视为等于相同值的整数。 相反 containsStrict 方法则是使用「严格」比较进行过滤。 #7.containsStrict方法 #此方法和 contains 方法类似,但是它却是使用了「严格」来比较所有值。 #基本使用 $collection = collect([ ['product' => 'Desk', 'price' => 200], ['product' => 'Chair', 'price' => 100], ]); dd($collection->containsStrict('price', '100')); // false #如上例所示,数组值存在,但是值类型不一致也返回false #8. count方法,返回该集合内的项目总数。 $collection = collect([1, 2, 3, 4]); $collection->count(); // 4 #9.diff方法,将集合与其它集合或纯 PHP 数组进行值的比较,然后返回原集合中存在而给定集合中不存在的值。 $collection = collect([1, 2, 3, 4, 5]); $diff = $collection->diff([2, 4, 6, 8]); $diff->all(); // [1, 3, 5] #10.diffAssoc方法 #与另外一个集合或基于它的键和值的 PHP 数组进行比较。 #这个方法会返回原集合不存在于给定集合中的键值对。 $collection = collect([ 'color' => 'orange', 'type' => 'fruit', 'remain' => 6 ]); $diff = $collection->diffAssoc([ 'color' => 'yellow', 'type' => 'fruit', 'remain' => 3, 'used' => 6 ]); $diff->all(); // ['color' => 'orange', 'remain' => 6] #11.diffKeys方法 #与另外一个集合或 PHP 数组的「键」进行比较,然后返回原集合中存在而#给定的集合中不存在「键」所对应的键值对。 $collection = collect([ 'one' => 10, 'two' => 20, 'three' => 30, 'four' => 40, 'five' => 50, ]); $diff = $collection->diffKeys([ 'two' => 2, 'four' => 4, 'six' => 6, 'eight' => 8, ]); $diff->all(); // ['one' => 10, 'three' => 30, 'five' => 50] #12.each方法 #迭代集合中的内容并将其传递到回调函数中。 $collection = $collection->each(function ($item, $key) { // }); #如果要中断对内容的迭代,那就从回调中返回 false $collection = $collection->each(function ($item, $key) { if (/* some condition */) { return false; } }); #13.every方法,可用于验证集合中每一个元素都通过给定的真实测试。 collect([1, 2, 3, 4])->every(function ($value, $key) { return $value > 2; }); // false #14.except方法,返回集合中除了指定键以外的所有项目。 $collection = collect(['product_id' => 1, 'price' => 100, 'discount' => false]); $filtered = $collection->except(['price', 'discount']); $filtered->all(); // ['product_id' => 1] #15.filter方法,使用给定的回调函数过滤集合的内容,只留下那些通过给定#真实测试的内容。 $collection = collect([1, 2, 3, 4]); $filtered = $collection->filter(function ($value, $key) { return $value > 2; }); $filtered->all(); // [3, 4] #如果没有提供回调函数,集合中所有返回false的元素都会被移除: $collection = collect([1, 2, 3, null, false, '', 0, []]); $collection->filter()->all(); // [1, 2, 3] #与filter()相反的方法,可以查看reject。 #16.first方法 #返回集合中通过给定真实测试的第一个元素。 collect([1, 2, 3, 4])->first(function ($value, $key) { return $value > 2; }); // 3 #也可以不传入参数使用 first() 方法以获取集合中第一个元素。如果集合是空的,则会返回 null: collect([1, 2, 3, 4])->first(); // 1 #如果需要返回最后一个元素可以使用last()方法。 #17.flatMap方法,遍历集合并将其中的每个值传递到给定的回调。 #可以通过回调修改每个值的内容再返回出来,从而形成一个新的被修改过内容的集合。 #然后就可以用 all() 打印修改后的数组。 $collection = collect([ ['name' => 'Sally'], ['school' => 'Arkansas'], ['age' => 28] ]); $flattened = $collection->flatMap(function ($values) { return array_map('strtoupper', $values); }); $flattened->all(); // ['name' => 'SALLY', 'school' => 'ARKANSAS', 'age' => '28']; #18.flatten方法,将多维集合转为一维。 $collection = collect(['name' => 'taylor', 'languages' => ['php', 'javascript']]); $flattened = $collection->flatten(); $flattened->all(); // ['taylor', 'php', 'javascript']; #还可以选择性地传入「深度」参数: $collection = collect([ 'Apple' => [ ['name' => 'iPhone 6S', 'brand' => 'Apple'], ], 'Samsung' => [ ['name' => 'Galaxy S7', 'brand' => 'Samsung'] ], ]); $products = $collection->flatten(1); $products->values()->all(); /* [ ['name' => 'iPhone 6S', 'brand' => 'Apple'], ['name' => 'Galaxy S7', 'brand' => 'Samsung'], ] */ #在这个例子里,调用 flatten 方法时不传入深度参数的话也会将嵌套数组转成一维的,然后返回 ['iPhone 6S', 'Apple', 'Galaxy S7', 'Samsung'],传入深度参数能限制设置返回数组的层数。 #19.flip方法,将集合中的键和对应的数值进行互换。 $collection = collect(['name' => 'taylor', 'framework' => 'laravel']); $flipped = $collection->flip(); $flipped->all(); // ['taylor' => 'name', 'laravel' => 'framework'] #20.forget方法,通过给定的键来移除掉集合中对应的内容。 $collection = collect(['name' => 'taylor', 'framework' => 'laravel']); $collection->forget('name'); $collection->all(); // ['framework' => 'laravel'] #与大多数集合的方法不同,forget 不会返回修改过后的新集合;它会直接修改原来的集合。 #21.forPage方法,返回给定页码上显示的项目的新集合。这个方法接受页码#作为其第一个参数和每页显示的项目数作为其第二个参数。 $collection = collect([1, 2, 3, 4, 5, 6, 7, 8, 9]); $chunk = $collection->forPage(2, 3); $chunk->all(); // [4, 5, 6] #22.get方法,返回给定键的项目。如果该键不存在,则返回 null。 $collection = collect(['name' => 'taylor', 'framework' => 'laravel']); $value = $collection->get('name'); // taylor #可以选择性地传递默认值作为第二个参数: $collection = collect(['name' => 'taylor', 'framework' => 'laravel']); $value = $collection->get('foo', 'default-value'); // default-value #甚至可以将回调函数当作默认值。如果指定的键不存在,就会返回回调的结果: $collection = collect(['name' => 'taylor', 'framework' => 'laravel']); $collection->get('email', function () { return 'default-value'; }); // default-value #23.get方法,返回给定键的项目。如果该键不存在,则返回 null。 $collection = collect(['name' => 'taylor', 'framework' => 'laravel']); $value = $collection->get('name'); // taylor #可以选择性地传递默认值作为第二个参数: $collection = collect(['name' => 'taylor', 'framework' => 'laravel']); $value = $collection->get('foo', 'default-value'); // default-value #甚至可以将回调函数当作默认值。如果指定的键不存在,就会返回回调的结果: $collection = collect(['name' => 'taylor', 'framework' => 'laravel']); $collection->get('email', function () { return 'default-value'; }); // default-value #24.has方法,判断集合中是否存在给定的键。 $collection = collect(['account_id' => 1, 'product' => 'Desk']); $collection->has('product'); // true $collection->has('name'); // false #25.implode方法,合并集合中的项目。 $collection = collect([ ['account_id' => 1, 'product' => 'Desk'], ['account_id' => 2, 'product' => 'Chair'], ]); $collection->implode('product', ', '); // Desk, Chair #其参数取决于集合中项目的类型。 #如果集合包含数组或对象,你应该传入你希望连接的属性的键,以及你希#望放在值之间用来「拼接」的字符串 #集合包含简单的字符串或数值,只需要传入「拼接」用的字符串作为该方法的唯一参数即可 collect([1, 2, 3, 4, 5])->implode('-'); // '1-2-3-4-5' #26.intersect方法,从原集合中删除不在给定数组或集合中的任何值,最终#的集合会保留原集合的键。 $collection = collect(['Desk', 'Sofa', 'Chair']); $intersect = $collection->intersect(['Desk', 'Chair', 'Bookcase']); $intersect->all(); // [0 => 'Desk', 2 => 'Chair'] #不改变原数组或集合。 #27.intersectKey方法,删除原集合中不存在于给定数组或集合中的任何键。 $collection = collect([ 'serial' => 'UX301', 'type' => 'screen', 'year' => 2009 ]); $intersect = $collection->intersectKey([ 'reference' => 'UX404', 'type' => 'tab', 'year' => 2011 ]); $intersect->all(); // ['type' => 'screen', 'year' => 2009] #28.isEmpty方法,集合是空的,isEmpty 方法返回 true,否则返回 false。 collect([])->isEmpty(); // true #29.isNotEmtpy方法,集合不是空的,isNotEmpty 方法会返回 true:否则返回 false: collect([])->isNotEmpty(); // false #30.keyBy方法,以给定的键作为集合的键。如果多个项目具有相同的键,则只有最后一个项目会显示在新集合中。 $collection = collect([ ['product_id' => 'prod-100', 'name' => 'desk'], ['product_id' => 'prod-200', 'name' => 'chair'], ]); $keyed = $collection->keyBy('product_id'); $keyed->all(); /* [ 'prod-100' => ['product_id' => 'prod-100', 'name' => 'Desk'], 'prod-200' => ['product_id' => 'prod-200', 'name' => 'Chair'], ] */ #也可以传入一个回调方法,回调返回的值会作为该集合的键 $keyed = $collection->keyBy(function ($item) { return strtoupper($item['product_id']); }); $keyed->all(); /* [ 'PROD-100' => ['product_id' => 'prod-100', 'name' => 'Desk'], 'PROD-200' => ['product_id' => 'prod-200', 'name' => 'Chair'], ] */ #31.keys方法,返回集合的所有键。 $collection = collect([ 'prod-100' => ['product_id' => 'prod-100', 'name' => 'Desk'], 'prod-200' => ['product_id' => 'prod-200', 'name' => 'Chair'], ]); $keys = $collection->keys(); $keys->all(); // ['prod-100', 'prod-200'] #32.last方法,返回集合中通过给定真实测试的最后一个元素。 collect([1, 2, 3, 4])->last(function ($value, $key) { return $value < 3; }); // 2 #你也可以不传入参数调用 last 方法来获取集合中最后一个元素。如果集合是空的,返回 null: collect([1, 2, 3, 4])->last(); // 4 #如果需要返回第一个元素可以使用first()方法。 #33.map方法,遍历集合并将每一个值传入给定的回调。 #该回调可以任意修改项目并返回,从而形成新的被修改过项目的集合。 $collection = collect([1, 2, 3, 4, 5]); $multiplied = $collection->map(function ($item, $key) { return $item * 2; }); $multiplied->all(); // [2, 4, 6, 8, 10] #像其他集合方法一样,map 返回一个新的集合实例;它不会修改它所调用的集合。如果想改变原集合,得使用 transform 方法。 #它与 reduce() 的区别是 reduce() 传入集合或数组,返回的是单一值;而map()传入数组或集合,返回的依然是集合。 #34.mapWithKeys方法 遍历集合并将每个值传入给定的回调。回调应该返回包含一个键值对的关联数组 $collection = collect([ [ 'name' => 'John', 'department' => 'Sales', 'email' => 'john@example.com' ], [ 'name' => 'Jane', 'department' => 'Marketing', 'email' => 'jane@example.com' ] ]); $keyed = $collection->mapWithKeys(function ($item) { return [$item['email'] => $item['name']]; }); $keyed->all(); /* [ 'john@example.com' => 'John', 'jane@example.com' => 'Jane', ] */ #35.max方法,返回给定键的最大值。 $max = collect([['foo' => 10], ['foo' => 20]])->max('foo'); // 20 $max = collect([1, 2, 3, 4, 5])->max(); // 5 #36.median方法,方法返回给定键的中间值。 $median = collect([['foo' => 10], ['foo' => 10], ['foo' => 20], ['foo' => 40]])->median('foo'); // 15 $median = collect([1, 1, 2, 4])->median(); // 1.5
#37.merge方法,将给定数组或集合合并到原集合。 #如果给定项目中的字符串键与原集合中的字符串键匹配,给定的项目的值将会覆盖原集合中的值。 $collection = collect(['product_id' => 1, 'price' => 100]); $merged = $collection->merge(['price' => 200, 'discount' => false]); $merged->all(); // ['product_id' => 1, 'price' => 200, 'discount' => false] #如果给定的项目的键是数字,这些值将被追加到集合的末尾: $collection = collect(['Desk', 'Chair']); $merged = $collection->merge(['Bookcase', 'Door']); $merged->all(); // ['Desk', 'Chair', 'Bookcase', 'Door'] #38.min方法,返回给定键的最小值。 $min = collect([['foo' => 10], ['foo' => 20]])->min('foo'); // 10 $min = collect([1, 2, 3, 4, 5])->min(); // 1 #39.mode方法,返回给定键的众数值。 $mode = collect([['foo' => 10], ['foo' => 10], ['foo' => 20], ['foo' => 40]])->mode('foo'); // [10] $mode = collect([1, 1, 2, 4])->mode(); // [1] #40.nth方法,创建由每隔n个元素组成一个新的集合。 $collection = collect(['a', 'b', 'c', 'd', 'e', 'f']); $collection->nth(4); // ['a', 'e'] #也可以选择传入一个偏移位置作为第二个参数 $collection = collect(['a', 'b', 'c', 'd', 'e', 'f']); $collection->nth(4, 1);// ['b', 'f'] #41.only方法,返回集合中给定键的所有项目。 $collection = collect(['product_id' => 1, 'name' => 'Desk', 'price' => 100, 'discount' => false]); $filtered = $collection->only(['product_id', 'name']); $filtered->all(); // ['product_id' => 1, 'name' => 'Desk'] #与 only 相反的方法,请查看 except。 #42. partition方法 #可以和 PHP 中的 list() 方法结合使用,来分开通过指定条件的元素以及那#些不通过指定条件的元素。 $collection = collect([1, 2, 3, 4, 5, 6]); list($underThree, $aboveThree) = $collection->partition(function ($i) { return $i < 3; }); #43.pip方法,将集合传给给定的回调并返回结果。 $collection = collect([1, 2, 3]); $piped = $collection->pipe(function ($collection) { return $collection->sum(); }); // 6 #44.pluck方法,获取集合中给定键对应的所有值。 $collection = collect([ ['product_id' => 'prod-100', 'name' => 'Desk'], ['product_id' => 'prod-200', 'name' => 'Chair'], ]); $plucked = $collection->pluck('name'); $plucked->all(); // ['Desk', 'Chair'] #也可以通过传入第二个参数来指定生成的集合的键: $plucked = $collection->pluck('name', 'product_id'); $plucked->all(); // ['prod-100' => 'Desk', 'prod-200' => 'Chair'] #45.pop方法,移除并返回集合中的最后一个项目。 $collection = collect([1, 2, 3, 4, 5]); $collection->pop(); // 5 $collection->all(); // [1, 2, 3, 4] #46.prepend方法,将给定的值添加到集合的开头。 $collection = collect([1, 2, 3, 4, 5]); $collection->prepend(0); $collection->all(); // [0, 1, 2, 3, 4, 5] #也可以传递第二个参数来设置前置项的键 $collection = collect(['one' => 1, 'two' => 2]); $collection->prepend(0, 'zero'); $collection->all(); // ['zero' => 0, 'one' => 1, 'two' => 2] #47.pull方法,把给定键对应的值从集合中移除并返回。 $collection = collect(['product_id' => 'prod-100', 'name' => 'Desk']); $collection->pull('name'); // 'Desk' $collection->all(); // ['product_id' => 'prod-100'] #48.push方法,把给定值添加到集合的末尾 $collection = collect([1, 2, 3, 4]); $collection->push(5); $collection->all(); // [1, 2, 3, 4, 5] #49.put方法,在集合内设置给定的键值对。 $collection = collect(['product_id' => 1, 'name' => 'Desk']); $collection->put('price', 100); $collection->all(); // ['product_id' => 1, 'name' => 'Desk', 'price' => 100] #50.random方法.从集合中返回一个随机项。 $collection = collect([1, 2, 3, 4, 5]); $collection->random(); // 4 - (retrieved randomly) 可以选择性传入一个整数到 random 来指定要获取的随机项的数量。 当显式传递希望接收的数量时,则会返回项目的集合。 $collection = collect([1, 2, 3, 4, 5]); $random = $collection->random(3); $random->all(); // [0 => 1, 1 => 2, 4 => 5] - (retrieved randomly) #51.reduce方法,将每次迭代的结果传递给下一次迭代直到集合减少为单个值。 $collection = collect([1, 2, 3]); $total = $collection->reduce(function ($carry, $item) { return $carry + $item; }); // 6 第一次迭代时 $carry 的数值为 null; 也可以通过传入第二个参数到 reduce() 来指定它的初始值: $collection->reduce(function ($carry, $item) { return $carry + $item; }, 4); // 10 它与 map() 的区别是 map() 传入集合或数组,返回也是集合;而reduce()传入数组或集合,返回单一值。 #52.reject方法,使用指定的回调过滤集合。如果回调返回 true ,就会把对应的项目从集合中移除。 $collection = collect([1, 2, 3, 4]); $filtered = $collection->reject(function ($value, $key) { return $value > 2; }); $filtered->all(); // [1, 2] 与 reject() 相反的方法,查看 filter方法。 #53.reverse方法.倒转集合中项目的顺序。 $collection = collect([1, 2, 3, 4, 5]); $reversed = $collection->reverse(); $reversed->all(); // [5, 4, 3, 2, 1] #54.search方法,搜索给定的值并返回它的键。如果找不到,则返回 false。 $collection = collect([2, 4, 6, 8]); $collection->search(4); // 1 搜索使用「宽松」比较完成,这意味着具有整数值的字符串会被认为等于相同值的整数。 要使用「严格」比较,就传入 true 作为该方法的第二个参数: $collection = collect([2, 4, 6, 8]); $collection->search('4', true); // false 另外,可以通过回调来搜索第一个通过真实测试的项目: $collection = collect([2, 4, 6, 8]); $collection->search(function ($item, $key) { return $item > 5; }); // 2 #55.shift方法,移除并返回集合的第一个项目。 $collection = collect([1, 2, 3, 4, 5]); $collection->shift(); // 1 $collection->all(); // [2, 3, 4, 5] #56.shuffle方法,随机排序集合中的项目。 $collection = collect([1, 2, 3, 4, 5]); $shuffled = $collection->shuffle(); $shuffled->all(); // [3, 2, 5, 1, 4] - (generated randomly) #57.slice方法,返回集合中给定值后面的部分。 $collection = collect([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); $slice = $collection->slice(4); $slice->all(); // [5, 6, 7, 8, 9, 10] 如果想限制返回内容的大小,就将期望的大小作为第二个参数传递给方法。 $collection = collect([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); $slice = $collection->slice(4, 2); $slice->all(); // [5, 6] #默认情况下,返回的内容将会保留原始键。 #假如不希望保留原始的键,可以使用 values 方法来重新建立索引。 #58.sort方法,对集合进行排序。序后的集合保留着原数组的键,所以在这个例子中我们使用 values 方法来把键重置为连续编号的索引。 $collection = collect([5, 3, 1, 2, 4]); $sorted = $collection->sort(); $sorted->values()->all(); // [1, 2, 3, 4, 5] 如果有更高级的排序需求,你可以传入回调来用你自己的算法进行排序。请参阅 PHP 文档的 usort,这是集合的 sort 方法在底层所调用的。 如果要对嵌套数组或对象的集合进行排序,参考 sortBy 和 sortByDesc 方法。 #59.sortBy方法,以给定的键对集合进行排序。 排序后的集合保留了原数组键,所以在这个例子中,我们使用 values 方法将键重置为连续编号的索引。 $collection = collect([ ['name' => 'Desk', 'price' => 200], ['name' => 'Chair', 'price' => 100], ['name' => 'Bookcase', 'price' => 150], ]); $sorted = $collection->sortBy('price'); $sorted->values()->all(); /* [ ['name' => 'Chair', 'price' => 100], ['name' => 'Bookcase', 'price' => 150], ['name' => 'Desk', 'price' => 200], ] */ 还可以传入自己的回调以决定如何对集合的值进行排序 $collection = collect([ ['name' => 'Desk', 'colors' => ['Black', 'Mahogany']], ['name' => 'Chair', 'colors' => ['Black']], ['name' => 'Bookcase', 'colors' => ['Red', 'Beige', 'Brown']], ]); $sorted = $collection->sortBy(function ($product, $key) { return count($product['colors']); }); $sorted->values()->all(); /* [ ['name' => 'Chair', 'colors' => ['Black']], ['name' => 'Desk', 'colors' => ['Black', 'Mahogany']], ['name' => 'Bookcase', 'colors' => ['Red', 'Beige', 'Brown']], ] */ #60.sortByDesc方法,与 sortBy 方法一样,但是会以相反的顺序来对集合进行排序。 $collection = collect([ ['name' => 'Desk', 'colors' => ['Black', 'Mahogany']], ['name' => 'Chair', 'colors' => ['Black']], ['name' => 'Bookcase', 'colors' => ['Red', 'Beige', 'Brown']], ]); $sorted = $collection->sortBy(function ($product, $key) { return count($product['colors']); }); $sorted->values()->all(); /* [ ['name' => 'Bookcase','colors' => ['Red', 'Beige', 'Brown']], ['name' => 'Desk', 'colors' => ['Black', 'Mahogany']], ['name' => 'Chair', 'colors' => ['Black']] ]; */ #61.splice方法,删除并返回从给定值后的内容,原集合也会受到影响。 $collection = collect([1, 2, 3, 4, 5]); $chunk = $collection->splice(2); $chunk->all(); // [3, 4, 5] $collection->all(); // [1, 2] 如果输入的值不存在则原集合内容不变。 可以传入第二个参数以限制被删除内容的大小。 $collection = collect([1, 2, 3, 4, 5]); $chunk = $collection->splice(2, 1); $chunk->all(); // [3] $collection->all(); // [1, 2, 4, 5] 也可以传入含有新项目的第三个参数来代替集合中删除的项目。 $collection = collect([1, 2, 3, 4, 5]); $chunk = $collection->splice(2, 1, [10, 11]); $chunk->all(); // [3] $collection->all(); // [1, 2, 10, 11, 4, 5] #62.split方法,将集合按给定的值拆分。 $collection = collect([1, 2, 3, 4, 5]); $groups = $collection->split(3); $groups->toArray(); // [[1, 2], [3, 4], [5]] #63.sum方法,返回集合内所有项目的总和。 collect([1, 2, 3, 4, 5])->sum(); // 15 如果集合包含嵌套数组或对象,则应该传入一个键来指定要进行求和的值 $collection = collect([ ['name' => 'JavaScript: The Good Parts', 'pages' => 176], ['name' => 'JavaScript: The Definitive Guide', 'pages' => 1096], ]); $collection->sum('pages'); // 1272 另外,也可以传入回调来决定要用集合中的哪些值进行求和 $collection = collect([ ['name' => 'Chair', 'colors' => ['Black']], ['name' => 'Desk', 'colors' => ['Black', 'Mahogany']], ['name' => 'Bookcase', 'colors' => ['Red', 'Beige', 'Brown']], ]); $collection->sum(function ($product) { return count($product['colors']); }); // 6 #64.take方法,返回给定数量项目的新集合 $collection = collect([0, 1, 2, 3, 4, 5]); $chunk = $collection->take(3); $chunk->all(); // [0, 1, 2] 也可以传入负整数从集合末尾开始获取指定数量的项目: $collection = collect([0, 1, 2, 3, 4, 5]); $chunk = $collection->take(-2); $chunk->all(); // [4, 5] #65.tap方法,将集合传递给回调,在特定点「tap」集合。此举能对集合中的项目执行某些操作,而不影响集合本身。 collect([2, 4, 3, 1, 5]) ->sort() ->tap(function ($collection) { Log::debug('Values after sorting', $collection->values()->toArray()); }) ->shift(); // 1 #66.times方法,通过回调在给定次数内创建一个新的集合。 $collection = Collection::times(10, function ($number) { return $number * 9; }); $collection->all(); // [9, 18, 27, 36, 45, 54, 63, 72, 81, 90] 使用这个方法可以与工厂结合使用创建出 Eloquent 模型 $categories = Collection::times(3, function ($number) { return factory(Category::class)->create(['name' => 'Category #'.$number]); }); $categories->all(); /* [ ['id' => 1, 'name' => 'Category #1'], ['id' => 2, 'name' => 'Category #2'], ['id' => 3, 'name' => 'Category #3'], ] */ #67.toArray方法,将集合转换成 PHP 数组。如果集合的值是 Eloquent 模型,那也会被转换成数组。 $collection = collect(['name' => 'Desk', 'price' => 200]); $collection->toArray(); /* [ ['name' => 'Desk', 'price' => 200], ] */ toArray 也会将所有集合的嵌套对象转换为数组。 如果你想获取原数组,就改用 all() 方法。 #68.toJson方法,将集合转换成 JSON 字符串。 $collection = collect(['name' => 'Desk', 'price' => 200]); $collection->toJson(); // '{"name":"Desk", "price":200}' #69.transform方法,迭代集合并对集合内的每个项目调用给定的回调。而集合的内容也会被回调返回的值取代。 $collection = collect([1, 2, 3, 4, 5]); $collection->transform(function ($item, $key) { return $item * 2; }); $collection->all(); // [2, 4, 6, 8, 10] 与大多数集合的方法不同,transform 会修改集合本身。 如果你想创建新的集合,就改用 map 方法。 #70.union方法,将给定的数组添加到集合中。如果给定的数组中含有与原集合一样的键,则原集合的值不会被改变。 $collection = collect([1 => ['a'], 2 => ['b']]); $union = $collection->union([3 => ['c'], 1 => ['b']]); $union->all(); // [1 => ['a'], 2 => ['b'], 3 => ['c']] #71.unique方法,返回集合中所有唯一的项目。返回的集合保留着原数组的键,所以在这个例子中,我们会使用 values 方法来把键重置为连续编号的索引。 $collection = collect([1, 1, 2, 2, 3, 4, 2]); $unique = $collection->unique(); // [ 0 => 1, 2 => 2, 4 => 3, 5 => 4]; $unique->values()->all(); // [1, 2, 3, 4] 处理嵌套数组或对象时,你可以指定用来决定唯一性的键: $collection = collect([ ['name' => 'iPhone 6', 'brand' => 'Apple', 'type' => 'phone'], ['name' => 'iPhone 5', 'brand' => 'Apple', 'type' => 'phone'], ['name' => 'Apple Watch', 'brand' => 'Apple', 'type' => 'watch'], ['name' => 'Galaxy S6', 'brand' => 'Samsung', 'type' => 'phone'], ['name' => 'Galaxy Gear', 'brand' => 'Samsung', 'type' => 'watch'], ]); $unique = $collection->unique('brand'); $unique->values()->all(); /* [ ['name' => 'iPhone 6', 'brand' => 'Apple', 'type' => 'phone'], ['name' => 'Galaxy S6', 'brand' => 'Samsung', 'type' => 'phone'], ] */ 你也可以传入自己的回调来确定项目的唯一性: $collection = collect([ ['name' => 'iPhone 6', 'brand' => 'Apple', 'type' => 'phone'], ['name' => 'iPhone 5', 'brand' => 'Apple', 'type' => 'phone'], ['name' => 'Apple Watch', 'brand' => 'Apple', 'type' => 'watch'], ['name' => 'Galaxy S6', 'brand' => 'Samsung', 'type' => 'phone'], ['name' => 'Galaxy Gear', 'brand' => 'Samsung', 'type' => 'watch'], ]); $unique = $collection->unique(function ($item) { return $item['brand'].$item['type']; }); $unique->values()->all(); /* [ ['name' => 'iPhone 6', 'brand' => 'Apple', 'type' => 'phone'], ['name' => 'Apple Watch', 'brand' => 'Apple', 'type' => 'watch'], ['name' => 'Galaxy S6', 'brand' => 'Samsung', 'type' => 'phone'], ['name' => 'Galaxy Gear', 'brand' => 'Samsung', 'type' => 'watch'], ] */ 在检查项目值时 unique 方法使用的是「宽松」比较,意味着具有整数值的字符串将被视为等于相同值的整数。使用 uniqueStrict 可以进行「严格」比较 。 #72.uniqueStrict方法,方法的使用和 unique 方法类似,只是使用了「严格」比较来过滤。 #73.values方法,返回键被重置为连续编号的新集合。 $collection = collect([ 10 => ['product' => 'Desk', 'price' => 200], 11 => ['product' => 'Desk', 'price' => 200] ]); $values = $collection->values(); $values->all(); /* [ 0 => ['product' => 'Desk', 'price' => 200], 1 => ['product' => 'Desk', 'price' => 200], ] */ #74.when方法,当传入的第一个参数为 true 的时,将执行给定的回调。 $collection = collect([1, 2, 3]); $collection->when(true, function ($collection) { return $collection->push(4); }); $collection->all(); // [1, 2, 3, 4] #75.where方法,通过给定的键值过滤集合。 $collection = collect([ ['product' => 'Desk', 'price' => 200], ['product' => 'Chair', 'price' => 100], ['product' => 'Bookcase', 'price' => 150], ['product' => 'Door', 'price' => 100], ]); $filtered = $collection->where('price', 100); $filtered->all(); /* [ ['product' => 'Chair', 'price' => 100], ['product' => 'Door', 'price' => 100], ] */ 比较数值的时候,where 方法使用「宽松」比较,意味着具有整数值的字符串将被认为等于相同值的整数。 使用 whereStrict 方法来进行「严格」比较过滤。 #76.whereStrict方法 方法与 where 方法一样;但是会以「严格」比较来匹配所有值。 #77.whereIn方法,通过给定的键值数组来过滤集合。 $collection = collect([ ['product' => 'Desk', 'price' => 200], ['product' => 'Chair', 'price' => 100], ['product' => 'Bookcase', 'price' => 150], ['product' => 'Door', 'price' => 100], ]); $filtered = $collection->whereIn('price', [150, 200]); $filtered->all(); /* [ ['product' => 'Bookcase', 'price' => 150], ['product' => 'Desk', 'price' => 200], ] */ #78.whereInStrict方法,此方法的使用和 whereIn 方法类似,只是使用了「严格」比较来匹配所有值。 #79.whereNotIn方法 通过集合中不包含的给定键值对进行: $collection = collect([ ['product' => 'Desk', 'price' => 200], ['product' => 'Chair', 'price' => 100], ['product' => 'Bookcase', 'price' => 150], ['product' => 'Door', 'price' => 100], ]); $filtered = $collection->whereNotIn('price', [150, 200]); $filtered->all(); /* [ ['product' => 'Chair', 'price' => 100], ['product' => 'Door', 'price' => 100], ] */ #80.whereNotInStrict方 此方法的使用和 whereNotIn 方法类似,只是使用了「严格」比较来匹配所有值 #81.zip方法,将给定数组的值与相应索引处的原集合的值合并在一起: $collection = collect(['Chair', 'Desk']); $zipped = $collection->zip([100, 200]); $zipped->all(); // [['Chair', 100], ['Desk', 200]]
4.在项目中单独使用
安装
Laravel中的Collection使用Composer管理,所以我们可以在项目中使用composer安装到非Laravel项目中,比如我们新建一个collections目录,通过下面使用命令安装
mkdir collections && cd collections composer require illuminate/support
执行完上面的命令将得到所需要的package。
使用
<?php // 引入package require __DIR__ . '/vendor/autoload.php';
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~