whereIn 删选数据
$colletion = collect([[
'orgId' => 1,
'name' => '公司名',
'icon' => 'icon1'
],[
'orgId' => 2,
'name' => '公司名',
'icon' => 'icon2'
],[
'orgId' => 3,
'name' => '公司名',
'icon' => 'icon3'
]]);
$newCollection = $colletion->whereIn('orgId', [2,3]);
dd($newCollection);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
where/whereStrict 删选数据
$colletion = collect([[
'orgId' => 1,
'name' => '公司名',
'icon' => 'icon1'
],[
'orgId' => 2,
'name' => '公司名',
'icon' => 'icon2'
],[
'orgId' => 3,
'name' => '公司名',
'icon' => 'icon3'
]]);
$newCollection = $colletion->where('orgId', 1);
dd($newCollection);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
toJson 集合转JSON 基本上来说不需要
$colletion = collect([[
'orgId' => 1,
'name' => '公司名',
'icon' => 'icon1'
],[
'orgId' => 2,
'name' => '公司名',
'icon' => 'icon2'
],[
'orgId' => 3,
'name' => '公司名',
'icon' => 'icon3'
]]);
dd($colletion->toJson());
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
对某一项取和
$colletion = collect([[
'orgId' => 1,
'name' => '公司名',
'icon' => 'icon1'
],[
'orgId' => 2,
'name' => '公司名',
'icon' => 'icon2'
],[
'orgId' => 3,
'name' => '公司名',
'icon' => 'icon3'
]]);
dd($colletion->sum('orgId'));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
集合分页取数据 splice
$colletion = collect([[
'orgId' => 1,
'name' => '公司名',
'icon' => 'icon1'
],[
'orgId' => 2,
'name' => '公司名',
'icon' => 'icon2'
],[
'orgId' => 3,
'name' => '公司名',
'icon' => 'icon3'
]]);
dd($colletion->splice(1,10));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
sort集合排序 sortBy/sortByDesc
$colletion = collect([[
'orgId' => 1,
'name' => '公司名',
'icon' => 'icon1'
],[
'orgId' => 2,
'name' => '公司名',
'icon' => 'icon2'
],[
'orgId' => 3,
'name' => '公司名',
'icon' => 'icon3'
]]);
$newCollection = $colletion->sortByDesc('icon');
dd($newCollection);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
search 搜索某一项在集合中是否存在
$colletion = collect([[
'orgId' => 1,
'name' => '公司名',
'icon' => 'icon1'
],[
'orgId' => 2,
'name' => '公司名',
'icon' => 'icon2'
],[
'orgId' => 3,
'name' => '公司名',
'icon' => 'icon3'
]]);
$newCollection = $colletion->search(function($item){
return $item['orgId'] == 4;
});
dd($newCollection);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
集合数据反转 reverse
$colletion = collect([[
'orgId' => 1,
'name' => '公司名',
'icon' => 'icon1'
],[
'orgId' => 2,
'name' => '公司名',
'icon' => 'icon2'
],[
'orgId' => 3,
'name' => '公司名',
'icon' => 'icon3'
]]);
$newCollection = $colletion->reverse();
dd($newCollection);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
在集合中添加一条数据 push
$colletion = collect([[
'orgId' => 1,
'name' => '公司名',
'icon' => 'icon1'
],[
'orgId' => 2,
'name' => '公司名',
'icon' => 'icon2'
],[
'orgId' => 3,
'name' => '公司名',
'icon' => 'icon3'
]]);
$newCollection = $colletion->push([
'orgId' => 4,
'name' => '心动',
'icon' => 'icon4'
]);
dd($newCollection);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
pull 方法通过键从集合中移除并返回数据项
$colletion = collect([[
'orgId' => 1,
'name' => '公司名',
'icon' => 'icon1'
],[
'orgId' => 2,
'name' => '公司名',
'icon' => 'icon2'
],[
'orgId' => 3,
'name' => '公司名',
'icon' => 'icon3'
]]);
$newCollection = $colletion->keyBy(function($item){
return $item['orgId'];
});
$ttCollection = $newCollection->pull('1'); 返回删除项
dd($newCollection); 原集合已经改变
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
prepend 添加数据到集合
$colletion = collect([[
'orgId' => 1,
'name' => '公司名',
'icon' => 'icon1'
],[
'orgId' => 2,
'name' => '公司名',
'icon' => 'icon2'
],[
'orgId' => 3,
'name' => '公司名',
'icon' => 'icon3'
]]);
$newCollection = $colletion->prepend([
'orgId' => 4,
'name' => '哈哈',
'icon' => 'icon4'
]);
dd($newCollection);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
pluck 方法为给定键获取所有集合值:
$colletion = collect([[
'orgId' => 1,
'name' => '公司名',
'icon' => 'icon1'
],[
'orgId' => 2,
'name' => '公司名',
'icon' => 'icon2'
],[
'orgId' => 3,
'name' => '公司名',
'icon' => 'icon3'
]]);
$newCollection = $colletion->pluck('name');
dd($newCollection);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
返回某一项的最大值 max
$colletion = collect([[
'orgId' => 1,
'name' => '公司名',
'icon' => 'icon1'
],[
'orgId' => 2,
'name' => '公司名',
'icon' => 'icon2'
],[
'orgId' => 3,
'name' => '公司名',
'icon' => 'icon3'
]]);
dd($colletion->max('orgId'));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
mapwithkeys 可以用来返回某些列的数据 mapWithKeys
$colletion = collect([[
'orgId' => 1,
'name' => '公司名',
'icon' => 'icon1'
],[
'orgId' => 2,
'name' => '公司名',
'icon' => 'icon2'
],[
'orgId' => 3,
'name' => '公司名',
'icon' => 'icon3'
]]);
$newCollection = $colletion->mapWithKeys(function($item){
return [['orgId' => $item['orgId'], 'name' => $item['name']]];
});
dd($newCollection);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
修改集合 map
$colletion = collect([[
'orgId' => 1,
'name' => '公司名',
'icon' => 'icon1'
],[
'orgId' => 2,
'name' => '公司名',
'icon' => 'icon2'
],[
'orgId' => 3,
'name' => '公司名',
'icon' => 'icon3'
]]);
$newCollection = $colletion->map(function($item){
$item['icon'] = 'hehe';
return $item;
});
var_dump($newCollection);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
取出嵌套集合的key keyBy
$colletion = collect([[
'orgId' => 1,
'name' => '公司名',
'icon' => 'icon1'
],[
'orgId' => 2,
'name' => '公司名',
'icon' => 'icon2'
],[
'orgId' => 3,
'name' => '公司名',
'icon' => 'icon3'
]]);
$orgCollection = $colletion->keyBy(function($item){
return $item['orgId'];
});
var_dump($orgCollection->keys());
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
为嵌套集合生成Key keyBy
$colletion = collect([[
'orgId' => 1,
'name' => '公司名',
'icon' => 'icon1'
],[
'orgId' => 2,
'name' => '公司名',
'icon' => 'icon2'
],[
'orgId' => 3,
'name' => '公司名',
'icon' => 'icon3'
]]);
$orgCollection = $colletion->keyBy(function($item){
return $item['orgId'];
});
var_dump($orgCollection);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
数组是否为空 isEmpty
$colletion = collect([[
'orgId' => 1,
'name' => '公司名',
'icon' => 'icon1'
],[
'orgId' => 2,
'name' => '公司名',
'icon' => 'icon2'
],[
'orgId' => 3,
'name' => '公司名',
'icon' => 'icon3'
]]);
var_dump($colletion->isEmpty());
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
获取某一列的数据,返回的是字符串 implode
$colletion = collect([[
'orgId' => 1,
'name' => '公司名',
'icon' => 'icon1'
],[
'orgId' => 2,
'name' => '公司名',
'icon' => 'icon2'
],[
'orgId' => 3,
'name' => '公司名',
'icon' => 'icon3'
]]);
$newString = $colletion->implode('name', ',');
var_dump(json_decode($newString));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
数据分页 forPage
$colletion = collect([[
'orgId' => 1,
'name' => '公司名',
'icon' => 'icon1'
],[
'orgId' => 2,
'name' => '公司名',
'icon' => 'icon2'
],[
'orgId' => 3,
'name' => '公司名',
'icon' => 'icon3'
]]);
$newCollection = $colletion->forPage(1,2);
var_dump($newCollection);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
获取集合的第一个元素, 可以根据条件来删选 first
$colletion = collect([[
'orgId' => 1,
'name' => '公司名',
'icon' => 'icon1'
],[
'orgId' => 2,
'name' => '公司名',
'icon' => 'icon2'
],[
'orgId' => 3,
'name' => '公司名',
'icon' => 'icon3'
]]);
$newCollection = $colletion->first(function($item){
return $item['orgId']>2;
});
var_dump($newCollection);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
过滤条件, 删选数据 filter
$colletion = collect([[
'orgId' => 1,
'name' => '公司名',
'icon' => 'icon1'
],[
'orgId' => 2,
'name' => '公司名',
'icon' => 'icon2'
],[
'orgId' => 3,
'name' => '公司名',
'icon' => 'icon3'
]]);
$newCollection = $colletion->filter(function($item) {
return $item['orgId'] == 2;
});
var_dump($newCollection);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
迭代每一个数据项 each
$colletion = collect([[
'orgId' => 1,
'name' => '公司名',
'icon' => 'icon1'
],[
'orgId' => 2,
'name' => '公司名',
'icon' => 'icon2'
],[
'orgId' => 3,
'name' => '公司名',
'icon' => 'icon3'
]]);
$colletion->each(function($item, $key){
if ($item['orgId'] == 1) { 暂停迭代
return false;
}
});
var_dump($colletion);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
集合长度 count
$colletion = collect([[
'orgId' => 1,
'name' => '公司名',
'icon' => 'icon1'
],[
'orgId' => 2,
'name' => '公司名',
'icon' => 'icon2'
],[
'orgId' => 3,
'name' => '公司名',
'icon' => 'icon3'
]]);
var_dump(count($colletion));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
是否包含某个值 contains
$colletion = collect([[
'orgId' => 1,
'name' => '公司名',
'icon' => 'icon1'
],[
'orgId' => 2,
'name' => '公司名',
'icon' => 'icon2'
],[
'orgId' => 3,
'name' => '公司名',
'icon' => 'icon3'
]]);
var_dump($colletion->contains(['orgId'], 4));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
返回某一项的平均值 avg
$colletion = collect([[
'orgId' => 1,
'name' => '公司名',
'icon' => 'icon1'
],[
'orgId' => 2,
'name' => '公司名',
'icon' => 'icon2'
],[
'orgId' => 3,
'name' => '公司名',
'icon' => 'icon3'
]]);
echo $colletion->avg('orgId');
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
集合删选 reject
$colletion = collect([[
'orgId' => 1,
'name' => '公司名',
'icon' => 'icon1'
],[
'orgId' => 2,
'name' => '公司名',
'icon' => 'icon2'
],[
'orgId' => 3,
'name' => '公司名',
'icon' => 'icon3'
]]);
var_dump($colletion);
$newCollection = $colletion->reject(function($item){
return $item['orgId'] == 1;
}) ;
var_dump($newCollection);
var_dump($colletion);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
集合转数组 toArray
$colletion = collect([[
'orgId' => 1,
'name' => '公司名',
'icon' => 'icon1'
],[
'orgId' => 2,
'name' => '公司名',
'icon' => 'icon2'
],[
'orgId' => 3,
'name' => '公司名',
'icon' => 'icon3'
]]);
var_dump($colletion->toArray());
var_dump($colletion->all());
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16