zoukankan      html  css  js  c++  java
  • 关于 laravel 集合的使用

    常用的有

    count()

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

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

    forPage()

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

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

    map()

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

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

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

    filter()

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

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

    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]

    flatten()

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

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

    注意:collapse() 与 flatten() 效果是不相同的;

    last()

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

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

    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]

    pluck()

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

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

    search()

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

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

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

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

    sum()

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

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

    toArray()

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

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

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

    toJson()

    toJson方法将集合转化为JSON:

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

    unique()

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

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

    可查询资料  http://laravelacademy.org/post/178.html#toc_10

    【Tag: collection】http://laravelacademy.org/post/178.html#ipt_kb_toc_178_3      

    【 Laravel 5.1 文档 ] 服务 —— 集合】http://laravelacademy.org/post/178.html#ipt_kb_toc_178_49

     【 Laravel 5.4 文档 ] 综合话题 —— 集合】http://laravelacademy.org/post/6863.html

    世事洞明皆学问
  • 相关阅读:
    Java监控工具介绍,VisualVm ,JProfiler,Perfino,Yourkit,Perf4J,JProbe,Java微基准测试
    Java C# C语言中的占位符
    Java广度优先爬虫示例(抓取复旦新闻信息)
    如何用java获得字符串的ASCII值
    Java使用正则表达式取网页中的一段内容(以取Js方法为例)
    Java--使用多线程下载,断点续传技术原理(RandomAccessFile)
    使用HttpClient 4.3.4 自动登录并抓取中国联通用户基本信息和账单数据,GET/POST/Cookie
    Android学习---通过内容提供者(ContentProvider)操作另外一个应用私有数据库的内容
    Android学习---ListView的点击事件,simpleAdapter和arrayadapter,SimpleCursoAdapter的原理和使用
    Android学习---ListView和Inflater的使用,将一个布局文件转化为一个对象
  • 原文地址:https://www.cnblogs.com/Json159/p/9570903.html
Copyright © 2011-2022 走看看