laravel-collect

        一.作用:集合操作

 

   二.例子:如下所示

 1 <?php
 2 require 'vendor/autoload.php';
 3 use Illuminate\Support\Collection;
 4 
 5 
 6 $arr = [
 7     [
 8         'id' => 1,
 9         'name' => 'test',        
10     ],
11     [
12         'id' => 2,
13         'name' => 'test2',
14     ],
15     [
16         'id' => 3,
17         'name' => 'test3',
18     ],
19     [
20         'id' => 3,
21         'name' => 'test4',
22     ],
23 ];
24 $o = new Collection($arr);

 

   三.函数

  (1)初始化range(1, $number) 并执行callback,返回collection实例

1 $o = Collection::times(3, function ($val, $key) {
2     return 'val' . $val;
3 });

  (2)map array_map 传入key val两个参数

1 $arr = $o->map(function ($val, $key) {
2     echo 'key' . $key . PHP_EOL;
3     return $val;
4 })->all();
5 print_r($arr);

  (3)sum 求和

1 echo $o->sum('id');
2 echo $o->sum(function($info) {
3     return $info['id'];
4 });

  (4)avg 平均数

1 echo $o->avg('id');
2 echo $o->avg(function($info) {
3     return $info['id'];
4 });

  (5)median

1 echo $o->median('id');

  (6)mode 出现次数最多的元素

1 print_r($o->mode('id'));

  (7)collapse 所有元素merge到一起

1 print_r($o->collapse()->all());

  (8)contains 是否存在元素

1 echo $o->contains($arr[0]);
2 echo $o->contains('id', '=', 1);
3 echo $o->contains('id', '<', 10);

  (9)crossJoin 笛卡尔乘积

1 print_r($o->crossJoin([1, 2], [3, 4])->all());

  (10)diff 差集

1 $o = new Collection([1, 2]);
2 print_r($o->diff([1, 3])->all());

  (11)diffUsing array_udiff

1 $o = new Collection([1, 2]);
2 print_r($o->diffUsing([1, 3], function ($a, $b) {
3     return $a - $b;     //返回0即相等
4 })->all());

  (12)diffAssoc  array_diff_assoc 带键值

1 $o = new Collection([1, 2]);
2 print_r($o->diffAssoc([2, 1])->all());

  (13)diffAssocUsing array_diff_uassoc 传入的是键值

1 $o = new Collection([5, 6]);
2 print_r($o->diffAssocUsing([7, 8], function ($a, $b) {
3     echo $a . $b . PHP_EOL;
4     return $a - $b;     //返回0即相等
5 })->all());

  (14)diffKeys array_diff_key

1 $o = new Collection([1, 2, 3]);
2 print_r($o->diffKeys([1, 2])->all());

  (15)diffKeysUsing array_diff_ukey

1 $o = new Collection([1, 2, 3]);
2 print_r($o->diffKeysUsing([1, 2], function ($k1, $k2) {
3     return $k1 - $k2;
4 })->all());

  (16)reject 和filter差不多

1 $o = new Collection([1, 2, 3]);
2 print_r($o->reject(1)->all());
3 print_r($o->reject(function ($val, $key) {
4     return $val == 1;
5 })->all());

  (17)unique 去重

 1 //unique 去重
 2 $o = new Collection([1, 2, 3, 1]);
 3 print_r($o->unique()->all());
 4 
 5 //严格校验
 6 $o = new Collection([1, 2, 3, '1']);
 7 print_r($o->unique(null, true)->all());
 8 
 9 //某个键值过滤
10 $o = new Collection($arr);
11 print_r($o->unique('id', true)->all());
12 
13 //自定义
14 print_r($o->unique(function ($info) {
15     return $info['id'];
16 }, true)->all());

  (18)duplicates 可以理解为all - unique  也支持严格模式

 1 $o = new Collection([1, 2, 3, 1, 1, 2]);
 2 print_r($o->duplicates()->all());
 3 
 4 $o = new Collection($arr);
 5 print_r($o->duplicates('id')->all());       //只取id列
 6 
 7 //自定义
 8 $o->duplicates(function ($info) {
 9     return $info['id'];
10 })->all();

  (19)duplicatesStrict  同  $this->duplicates($callback, true);

  (20)except 删除传入键值

1 print_r($o->except([0, '1.id', '2.name']));

  (21)filter array_filter

1 print_r($o->filter(function ($info) {
2     return $info['id'] == 1;
3 })->all());

  (22)first 同 Arr::first

  (23)flatten 同 Arr::flatten

  (24)flip array_flip

1 $o = new Collection(['a' => 'b']);
2 print_r($o->flip()->all());

  (25)forget unset

1 print_r($o->forget([0, 1])->all());

  (26)get

1 print_r($o->get(0));
2 print_r($o->get(10, 'undefine'));

  (27)groupBy 分组 第二个参数表示是否保留键值

1 print_r($o->groupBy(['id', 'name'], true)->all());
2 print_r($o->groupBy(function ($val, $key) {
3     return [$val['id'], $val['name']];  //返回值表示将其分到哪些组  ps:一个值可以分到多个组
4 }, true)->all());

  (28)keyBy 设置某一列为键值

1 print_r($o->keyBy('id')->all());
2 print_r($o->keyBy(function ($item) {
3     return $item['id'] ?? '';
4 })->all());

  (29)has 是否存在 指定的键值

1 echo $o->has([0, 1, 2]);
2 echo $o->has([0, 1, 2, 5]);

  (30)implode

1 //如果元素是数组
2 print_r($o->implode('id', '@'));
3 echo PHP_EOL;
4 //非数组
5 print_r(collect([0, 1, 2])->implode('@'));

  (31)intersect 交集

1 print_r(collect([0, 1, 2])->intersect([1, 2])->all());

  (32)intersectByKeys

1 print_r(collect([
2     'a' => 0,
3     'b' => 1,
4 ])->intersectByKeys([
5     'a' => 9,
6 ])->all());

  (33)isEmpty

1 echo $o->isEmpty();

  (34)join 第二个参数表示最后一个元素的拼接符号

1 echo collect(['a', 'b', 'c'])->join('@');
2 echo collect(['a', 'b', 'c'])->join('@', '#');

  (35)keys

1 print_r($o->keys()->all());

  (36)last (callback的话  返回最后一个判定成立的元素)

1 print_r($o->last());
2 print_r($o->last(function ($info) {
3     return true;
4 }));

  (37)pluck 取key和value重新组成数组

print_r($o->pluck('name', 'id')->all());

  (38)mapToDictionary  mapToDictionary 生成一个二维字典  callback返回key => val key会聚合

1 print_r($o->mapToDictionary(function ($item, $key){
2     return ['new key' => 'new val'];
3 }));

  (39)mapWithKeys 返回的数组会被merge

1 print_r($o->mapWithKeys(function ($item, $key) {
2     return [$key . '.id' => $item['id']];
3 }));

  (40)merge

print_r($o->merge(['a'])->all());

  (41)mergeRecursive  array_merge_recursive

print_r(collect(['a' => 1])->mergeRecursive(['a' => 2])->all());

  (42)combine

print_r(collect(['a', 'b', 'c'])->combine([1, 2, 3])->all());

  (43)union  会补上新增的键值

print_r($o->union([4 => 0])->all());

  (44)nth 设置step取值

print_r($o->nth(2)->all());

  (46)only 只返回部分键值

1 print_r($o->only([0, 2]));

  (47)pop

1 print_r($o->pop());
2 print_r($o->all());

  (48)prepend 从头新增一个元素  可指定键值

1 print_r($o->prepend(0)->all());
2 print_r($o->prepend(0, 'a')->all());

  (49)本实例push 追加

1 print_r($o->push(0, 1, 2)->all());

  (50)concat 追加 返回一个新的实例

print_r($o->concat([0, 1, 2])->all());

  (51)pull 返回并删除

1 print_r($o->pull(0));
2 print_r($o->all());

  (52)put 新增

print_r($o->put('a', 1)->all());

  (53)random 随机返回n个元素

1 print_r($o->random());
2 print_r($o->random(2)->all());

  (54)reduce array_reduce

1 echo $o->reduce(function($deal, $val) {
2     return $deal . $val['name'];
3 }, '');

  (55)replace 替换对应键值

1 print_r($o->replace(['aaa'])->all());

  (56)replaceRecursive  array_replace_recursive

  (57)reverse  array_reverse

  (58)search

1 $o = collect([1, 2, 3]);
2 echo $o->search(2);
3 echo $o->search(function ($item, $key) {
4     return $item == 2;
5 });

  (59)shift array_shift

  (60)shuffle  打乱数组

  (61)skip 跳过部分数组 print_r($o->skip(2));

  (62)skipUntil 跳过

1 print_r($o->skipUntil(function ($val, $k) {
2     if ($k == 1) {
3         return true;   
4     }
5 })->all());

  (63)skipWhile  跳过

1 print_r($o->skipWhile(function ($val, $k) {
2     if ($k == 1) {
3         return false;   
4     }
5     return true;
6 })->all());

  (65)slice array_slice

  (66)split  分组  参数为groups

  (67)chunk  分组  参数为size

  (68)sort uasort

  (69)sortDesc  arsort

  (70)sortBy sortByDesc  根据某个字段排序

  (71)sortKeys  sortKeysDesc 根据键值排序

  (72)splice array_splice

  (73)take 取前几个元素

  (74)takeUntil  takeWhile  和skipUntil差不多 一个是take一个是skip

  (75)transform 对item执行array_map

1 print_r($o->transform(function ($val, $k) {
2     return $val['id'];
3 })->all());

  (76)values array_values

  (77)zip

1 //     * e.g. new Collection([1, 2, 3])->zip([4, 5, 6]);
2 //     *      => [[1, 4], [2, 5], [3, 6]]
3 //     *
4 print_r($o->zip([1, 2, 3])->all());

  (78)pad  array_pad

  (79)countBy 分组

  (80)add 新增一个元素

  (81)toBase 复制一个实例

 

posted @ 2021-01-03 21:28  Dahouzi  阅读(567)  评论(0编辑  收藏  举报