Home > Backend Development > PHP Tutorial > [ Laravel 5.2 文档 ] 服务 -- 集合

[ Laravel 5.2 文档 ] 服务 -- 集合

WBOY
Release: 2016-06-23 13:17:17
Original
940 people have browsed it

1、简介

Illuminate\Support\Collection类为处理数组数据提供了平滑、方便的封装。例如,查看下面的代码,我们使用辅助函数 collect创建一个新的集合实例,为每一个元素运行 strtoupper函数,然后移除所有空元素:

$collection = collect(['taylor', 'abigail', null])->map(function ($name) {                  return strtoupper($name);              })->reject(function ($name) {                  return empty($name);              });
Copy after login

正如你所看到的, Collection类允许你使用方法链对底层数组执行匹配和减少操作,通常,没个 Collection方法都会返回一个新的 Collection实例。

2、创建集合

正如上面所提到的,辅助函数 collect为给定数组返回一个新的 Illuminate\Support\Collection实例,所以,创建集合很简单:

$collection = collect([1, 2, 3]);
Copy after login

默认情况下,Eloquent模型的集合总是返回 Collection实例,此外,不管是在何处,只要方法都可以自由使用 Collection类。

3、集合方法

本文档接下来的部分我们将会讨论 Collection类上每一个有效的方法,所有这些方法都可以以方法链的方式平滑的操作底层数组。此外,几乎每个方法返回一个新的 Collection实例,允许你在必要的时候保持原来的集合备份。

all()

all方法简单返回集合表示的底层数组:

collect([1, 2, 3])->all(); // [1, 2, 3]
Copy after login

avg()

avg方法返回所有集合项的平均值:

collect([1, 2, 3, 4, 5])->avg();// 3
Copy after login

如果集合包含嵌套的数组或对象,需要指定要使用的键以判定计算那些值的平均值:

$collection = collect([    ['name' => 'JavaScript: The Good Parts', 'pages' => 176],    ['name' => 'JavaScript: The Definitive Guide', 'pages' => 1096],]);$collection->avg('pages');// 636
Copy after login

chunk()

chunk方法将一个集合分割成多个小尺寸的小集合:

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

当处理栅栏系统如 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
Copy after login

collapse()

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]
Copy after login

contains()

contains方法判断集合是否包含一个给定项:

$collection = collect(['name' => 'Desk', 'price' => 100]);$collection->contains('Desk');// true$collection->contains('New York');// false
Copy after login

你还可以传递一个键值对到 contains方法,这将会判断给定键值对是否存在于集合中:

$collection = collect([    ['product' => 'Desk', 'price' => 200],    ['product' => 'Chair', 'price' => 100],]);$collection->contains('product', 'Bookcase');// false
Copy after login

最后,你还可以传递一个回调到 contains方法来执行自己的真实测试:

$collection = collect([1, 2, 3, 4, 5]);$collection->contains(function ($key, $value) {    return $value > 5;});// false
Copy after login

count()

count方法返回集合中所有项的数目:

$collection = collect([1, 2, 3, 4]);$collection->count();// 4
Copy after login

diff()

diff方法将集合和另一个集合或原生PHP数组作比较:

$collection = collect([1, 2, 3, 4, 5]);$diff = $collection->diff([2, 4, 6, 8]);$diff->all();// [1, 3, 5]
Copy after login

each()

each方法迭代集合中的数据项并传递每个数据项到给定回调:

$collection = $collection->each(function ($item, $key) {    //});
Copy after login

回调返回 false将会终止循环:

$collection = $collection->each(function ($item, $key) {    if (/* some condition */) {        return false;    }});
Copy after login

every()

every方法创建一个包含数组第 n-th个元素的新集合:

$collection = collect(['a', 'b', 'c', 'd', 'e', 'f']);$collection->every(4);// ['a', 'e']
Copy after login

还可以选择指定从第几个元素开始:

$collection->every(4, 1);// ['b', 'f']
Copy after login

except()

except方法返回集合中除了指定键的所有集合项:

$collection = collect(['product_id' => 1, 'name' => 'Desk', 'price' => 100, 'discount' => false]);$filtered = $collection->except(['price', 'discount']);$filtered->all();// ['product_id' => 1, 'name' => 'Desk']
Copy after login

与 except相对的是 only方法。

filter()

filter方法通过给定回调过滤集合,只有通过给定测试的数据项才会保留下来:

$collection = collect([1, 2, 3, 4]);$filtered = $collection->filter(function ($item) {    return $item > 2;});$filtered->all();// [3, 4]
Copy after login

和 filter相对的方法是 reject 。

first()

first方法返回通过测试集合的第一个元素:

collect([1, 2, 3, 4])->first(function ($key, $value) {    return $value > 2;});// 3
Copy after login

你还可以调用不带参数的 first方法来获取集合的第一个元素,如果集合是空的,返回null:

collect([1, 2, 3, 4])->first();// 1
Copy after login

flatten()

flatten方法将多维度的集合变成一维的:

$collection = collect(['name' => 'taylor', 'languages' => ['php', 'javascript']]);$flattened = $collection->flatten();$flattened->all();// ['taylor', 'php', 'javascript'];
Copy after login

flip()

flip方法将集合的键值做交换:

$collection = collect(['name' => 'taylor', 'framework' => 'laravel']);$flipped = $collection->flip();$flipped->all();// ['taylor' => 'name', 'laravel' => 'framework']
Copy after login

forget()

forget方法通过键从集合中移除数据项:

$collection = collect(['name' => 'taylor', 'framework' => 'laravel']);$collection->forget('name');$collection->all();// [framework' => 'laravel']
Copy after login

注意:不同于大多数的集合方法, forget不返回新的修改过的集合;它只修改所调用的集合。

forPage()

forPage方法返回新的包含给定页数数据项的集合:

$collection = collect([1, 2, 3, 4, 5, 6, 7, 8, 9])->forPage(2, 3);$collection->all();// [4, 5, 6]
Copy after login

该方法需要传入页数和每页显示数目参数。

get()

get方法返回给定键的数据项,如果不存在,返回null:

$collection = collect(['name' => 'taylor', 'framework' => 'laravel']);$value = $collection->get('name');// taylor
Copy after login

你可以选择传递默认值作为第二个参数:

$collection = collect(['name' => 'taylor', 'framework' => 'laravel']);$value = $collection->get('foo', 'default-value');// default-value
Copy after login

你甚至可以传递回调作为默认值,如果给定键不存在的话回调的结果将会返回:

$collection->get('email', function () {return 'default-value';});// default-value
Copy after login

groupBy()

groupBy方法通过给定键分组集合数据项:

$collection = collect([    ['account_id' => 'account-x10', 'product' => 'Chair'],    ['account_id' => 'account-x10', 'product' => 'Bookcase'],    ['account_id' => 'account-x11', 'product' => 'Desk'],]);$grouped = $collection->groupBy('account_id');$grouped->toArray();/*[    'account-x10' => [        ['account_id' => 'account-x10', 'product' => 'Chair'],        ['account_id' => 'account-x10', 'product' => 'Bookcase'],    ],    'account-x11' => [        ['account_id' => 'account-x11', 'product' => 'Desk'],    ],]*/
Copy after login

除了传递字符串key,还可以传递一个回调,回调应该返回分组后的值:

$grouped = $collection->groupBy(function ($item, $key) {    return substr($item['account_id'], -3);});$grouped->toArray();/*[    'x10' => [        ['account_id' => 'account-x10', 'product' => 'Chair'],        ['account_id' => 'account-x10', 'product' => 'Bookcase'],    ],    'x11' => [        ['account_id' => 'account-x11', 'product' => 'Desk'],    ],]*/
Copy after login

has()

has方法判断给定键是否在集合中存在:

$collection = collect(['account_id' => 1, 'product' => 'Desk']);$collection->has('email');// false
Copy after login

implode()

implode方法连接集合中的数据项。其参数取决于集合中数据项的类型。

如果集合包含数组或对象,应该传递你想要连接的属性键,以及你想要放在值之间的 “粘合”字符串:

$collection = collect([    ['account_id' => 1, 'product' => 'Desk'],    ['account_id' => 2, 'product' => 'Chair'],]);$collection->implode('product', ', ');// Desk, Chair
Copy after login

如果集合包含简单的字符串或数值,只需要传递“粘合”字符串作为唯一参数到该方法:

collect([1, 2, 3, 4, 5])->implode('-');// '1-2-3-4-5'
Copy after login

intersect()

intersect方法返回两个集合的交集:

$collection = collect(['Desk', 'Sofa', 'Chair']);$intersect = $collection->intersect(['Desk', 'Chair', 'Bookcase']);$intersect->all();// [0 => 'Desk', 2 => 'Chair']
Copy after login

正如你所看到的,结果集合只保持原来集合的键。

isEmpty()

如果集合为空的话 isEmpty方法返回 true;否则返回 false:

collect([])->isEmpty();// true
Copy after login

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'],    ]*/
Copy after login

如果多个数据项有同一个键,只有最后一个会出现在新的集合中。

你可以传递自己的回调,将会返回经过处理的键的值作为新的键:

$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'],    ]*/
Copy after login

keys()

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']
Copy after login

last()

last方法返回通过测试的集合的最后一个元素:

collect([1, 2, 3, 4])->last(function ($key, $value) {    return $value < 3;});// 2
Copy after login

还可以调用无参的 last方法来获取集合的最后一个元素。如果集合为空。返回 null:

collect([1, 2, 3, 4])->last();// 4
Copy after login

map()

map方法遍历集合并传递每个值给给定回调。该回调可以修改数据项并返回,从而生成一个新的经过修改的集合:

$collection = collect([1, 2, 3, 4, 5]);$multiplied = $collection->map(function ($item, $key) {    return $item * 2;});$multiplied->all();// [2, 4, 6, 8, 10]
Copy after login

注意:和大多数集合方法一样, map返回新的集合实例;它并不修改所调用的实例。如果你想要改变原来的集合,使用 transform方法。

max()

max方法返回集合中最大值:

$max = collect([['foo' => 10], ['foo' => 20]])->max('foo');// 20$max = collect([1, 2, 3, 4, 5])->max();// 5
Copy after login

merge()

merge方法合并给定数组到集合。该数组中的任何字符串键匹配集合中的字符串键的将会重写集合中的值:

$collection = collect(['product_id' => 1, 'name' => 'Desk']);$merged = $collection->merge(['price' => 100, 'discount' => false]);$merged->all();// ['product_id' => 1, 'name' => 'Desk', 'price' => 100, 'discount' => false]
Copy after login

如果给定数组的键是数字,数组的值将会附加到集合后面:

$collection = collect(['Desk', 'Chair']);$merged = $collection->merge(['Bookcase', 'Door']);$merged->all();// ['Desk', 'Chair', 'Bookcase', 'Door']
Copy after login

min()

min方法返回集合中的最小值:

$min = collect([['foo' => 10], ['foo' => 20]])->min('foo');// 10$min = collect([1, 2, 3, 4, 5])->min();// 1
Copy after login

only()

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']
Copy after login

与 only方法相对的是except 方法。

pluck()

pluck方法为给定键获取所有集合值:

$collection = collect([    ['product_id' => 'prod-100', 'name' => 'Desk'],    ['product_id' => 'prod-200', 'name' => 'Chair'],]);$plucked = $collection->pluck('name');$plucked->all();// ['Desk', 'Chair']
Copy after login

还可以指定你想要结果集合如何设置键:

$plucked = $collection->pluck('name', 'product_id');$plucked->all();// ['prod-100' => 'Desk', 'prod-200' => 'Chair']
Copy after login

pop()

pop方法移除并返回集合中最后面的数据项:

$collection = collect([1, 2, 3, 4, 5]);$collection->pop();// 5$collection->all();// [1, 2, 3, 4]
Copy after login

prepend()

prepend方法添加数据项到集合开头:

$collection = collect([1, 2, 3, 4, 5]);$collection->prepend(0);$collection->all();// [0, 1, 2, 3, 4, 5]
Copy after login

你还可以传递第二个参数到该方法用于设置前置项的键:

$collection = collect(['one' => 1, 'two', => 2]);$collection->prepend(0, 'zero');$collection->all();// ['zero' => 0, 'one' => 1, 'two', => 2]
Copy after login

pull()

pull方法通过键从集合中移除并返回数据项:

$collection = collect(['product_id' => 'prod-100', 'name' => 'Desk']);$collection->pull('name');// 'Desk'$collection->all();// ['product_id' => 'prod-100']
Copy after login

push()

push方法附加数据项到集合结尾:

$collection = collect([1, 2, 3, 4]);$collection->push(5);$collection->all();// [1, 2, 3, 4, 5]
Copy after login

put()

put方法在集合中设置给定键和值:

$collection = collect(['product_id' => 1, 'name' => 'Desk']);$collection->put('price', 100);$collection->all();// ['product_id' => 1, 'name' => 'Desk', 'price' => 100]
Copy after login

random()

random方法从集合中返回随机数据项:

$collection = collect([1, 2, 3, 4, 5]);$collection->random();// 4 - (retrieved randomly)
Copy after login

你可以传递一个整型数据到 random函数,如果该整型数值大于1,将会返回一个集合:

$random = $collection->random(3);$random->all();// [2, 4, 5] - (retrieved randomly)
Copy after login

reduce()

reduce方法用于减少集合到单个值,传递每个迭代结果到随后的迭代:

$collection = collect([1, 2, 3]);$total = $collection->reduce(function ($carry, $item) {    return $carry + $item;});// 6
Copy after login

在第一次迭代时 $carry的值是null;然而,你可以通过传递第二个参数到 reduce来指定其初始值:

$collection->reduce(function ($carry, $item) {    return $carry + $item;}, 4);// 10
Copy after login

reject()

reject方法使用给定回调过滤集合,该回调应该为所有它想要从结果集合中移除的数据项返回 true:

$collection = collect([1, 2, 3, 4]);$filtered = $collection->reject(function ($item) {    return $item > 2;});$filtered->all();// [1, 2]
Copy after login

和 reduce方法相对的方法是filter方法。

reverse()

reverse方法将集合数据项的顺序颠倒:

$collection = collect([1, 2, 3, 4, 5]);$reversed = $collection->reverse();$reversed->all();// [5, 4, 3, 2, 1]
Copy after login

search()

search方法为给定值查询集合,如果找到的话返回对应的键,如果没找到,则返回 false:

$collection = collect([2, 4, 6, 8]);$collection->search(4);// 1
Copy after login

上面的搜索使用的是松散比较,要使用严格比较,传递 true作为第二个参数到该方法:

$collection->search('4', true);// false
Copy after login

此外,你还可以传递自己的回调来搜索通过测试的第一个数据项:

$collection->search(function ($item, $key) {    return $item > 5;});// 2
Copy after login

shift()

shift方法从集合中移除并返回第一个数据项:

$collection = collect([1, 2, 3, 4, 5]);$collection->shift();// 1$collection->all();// [2, 3, 4, 5]
Copy after login

shuffle()

shuffle方法随机打乱集合中的数据项:

$collection = collect([1, 2, 3, 4, 5]);$shuffled = $collection->shuffle();$shuffled->all();// [3, 2, 5, 1, 4] // (generated randomly)
Copy after login

slice()

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]
Copy after login

如果你想要限制返回切片的尺寸,将尺寸值作为第二个参数传递到该方法:

$slice = $collection->slice(4, 2);$slice->all();// [5, 6]
Copy after login

返回的切片有新的、数字化索引的键,如果你想要保持原有的键,可以传递第三个参数 true到该方法。

sort()

sort方法对集合进行排序:

$collection = collect([5, 3, 1, 2, 4]);$sorted = $collection->sort();$sorted->values()->all();// [1, 2, 3, 4, 5]
Copy after login

排序后的集合保持原来的数组键,在本例中我们使用 values 方法重置键为连续编号索引。

要为嵌套集合和对象排序,查看 sortBy和 sortByDesc方法。

如果你需要更加高级的排序,你可以使用自己的算法传递一个回调给 sort方法。参考 PHP 官方文档关于 usort 的说明, sort方法底层正是调用了该方法。

sortBy()

sortBy方法通过给定键对集合进行排序:

$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],    ]*/
Copy after login

排序后的集合保持原有数组索引,在本例中,使用 values方法重置键为连续索引。

你还可以传递自己的回调来判断如何排序集合的值:

$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']],    ]*/
Copy after login

sortByDesc()

该方法和 sortBy用法相同,不同之处在于按照相反顺序进行排序。

splice()

splice方法在从给定位置开始移除并返回数据项切片:

$collection = collect([1, 2, 3, 4, 5]);$chunk = $collection->splice(2);$chunk->all();// [3, 4, 5]$collection->all();// [1, 2]
Copy after login

你可以传递参数来限制返回组块的大小:

$collection = collect([1, 2, 3, 4, 5]);$chunk = $collection->splice(2, 1);$chunk->all();// [3]$collection->all();// [1, 2, 4, 5]
Copy after login

此外,你可以传递第三个参数来包含新的数据项来替代从集合中移除的数据项:

$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]
Copy after login

sum()

sum方法返回集合中所有数据项的和:

collect([1, 2, 3, 4, 5])->sum();// 15
Copy after login

如果集合包含嵌套数组或对象,应该传递一个键用于判断对哪些值进行求和运算:

$collection = collect([    ['name' => 'JavaScript: The Good Parts', 'pages' => 176],    ['name' => 'JavaScript: The Definitive Guide', 'pages' => 1096],]);$collection->sum('pages');// 1272
Copy after login

此外,你还可以传递自己的回调来判断对哪些值进行求和:

$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
Copy after login

take()

take方法使用指定数目的数据项返回一个新的集合:

$collection = collect([0, 1, 2, 3, 4, 5]);$chunk = $collection->take(3);$chunk->all();// [0, 1, 2]
Copy after login

你还可以传递负数从集合末尾开始获取指定数目的数据项:

$collection = collect([0, 1, 2, 3, 4, 5]);$chunk = $collection->take(-2);$chunk->all();// [4, 5]
Copy after login

toArray()

toArray方法将集合转化为一个原生的 PHP 数组。如果集合的值是Eloquent 模型,该模型也会被转化为数组:

$collection = collect(['name' => 'Desk', 'price' => 200]);$collection->toArray();/*    [        ['name' => 'Desk', 'price' => 200],    ]*/
Copy after login

注意: toArray还将所有嵌套对象转化为数组。如果你想要获取底层数组,使用 all方法。

toJson()

toJson方法将集合转化为JSON:

$collection = collect(['name' => 'Desk', 'price' => 200]);$collection->toJson();// '{"name":"Desk","price":200}'
Copy after login

transform()

transform方法迭代集合并对集合中每个数据项调用给定回调。集合中的数据项将会被替代成从回调中返回的值:

$collection = collect([1, 2, 3, 4, 5]);$collection->transform(function ($item, $key) {    return $item * 2;});$collection->all();// [2, 4, 6, 8, 10]
Copy after login

注意:不同于大多数其它集合方法, transform修改集合本身,如果你想要创建一个新的集合,使用map方法。

unique()

unique方法返回集合中所有的唯一数据项:

$collection = collect([1, 1, 2, 2, 3, 4, 2]);$unique = $collection->unique();$unique->values()->all();// [1, 2, 3, 4]
Copy after login

返回的集合保持原来的数组键,在本例中我们使用values方法重置这些键为连续的数字索引。

处理嵌套数组或对象时,可以指定用于判断唯一的键:

$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'],    ]*/
Copy after login

你还可以指定自己的回调用于判断数据项唯一性:

$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'],    ]*/
Copy after login

values()

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],    ]*/
Copy after login

where()

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],]*/
Copy after login

检查数据项值时 where方法使用严格条件约束。使用whereLoose方法过滤松散约束。

whereLoose()

该方法和 where使用方法相同,不同之处在于 whereLoose在比较值的时候使用松散约束。

zip()

zip方法在于集合的值相应的索引处合并给定数组的值:

$collection = collect(['Chair', 'Desk']);$zipped = $collection->zip([100, 200]);$zipped->all();// [['Chair', 100], ['Desk', 200]]
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template