dsj2014 2016-10-24 07:55
浏览 14
已采纳

如何在已查询的集合中计算组内elequont对象的数量?

This is my current output

Collection {#794 ▼
  #items: array:8 [▼
    "IN" => Collection {#795 ▶}
    "NZ" => Collection {#787 ▶}
  ]}

I want the items to be hold the no of count for each codes like

"IN" => 4,
"NZ" => 3,

I know that I can directly write in a query like this

$query->groupBy('country_code')->orderBy('country_code', 'ASC');
    return $query->get([
        DB::raw('country_code as country_code'),
        DB::raw('COUNT(*) as "count"')
    ]);

But I want the output from a already queried collection to reduce multiple queries which is a collection.

Right now I am only able to group by on the collection like this

$collection->groupBy('country_code');
  • 写回答

1条回答 默认 最新

  • dsutuyxe088689 2016-10-24 09:26
    关注
    $b = $a->groupBy('country_code');
    

    You've done most of the job by proper grouping the data based on the country_code. Now it's just to iterate through the collection with a foreach, key, value and use the collections's count() method to count the number of elements stored under a given country_code

    foreach ($b as $countryCode => $items) {
        echo $items->count()."
    ";
    }
    

    Reproduce:

    php artisan ti
    Psy Shell v0.7.2 (PHP 7.0.8-0ubuntu0.16.04.3 — cli) by Justin Hileman
    >>> $cities = collect([['country_code' => 'pl', 'name' => 'Warszawa'], ['country_code' => 'pl', 'name' => 'Wrocław'], ['country_code' => 'de', 'name' => 'Berlin']]);
    => Illuminate\Support\Collection {#846
         all: [
           [
             "country_code" => "pl",
             "name" => "Warszawa",
           ],
           [
             "country_code" => "pl",
             "name" => "Wrocław",
           ],
           [
             "country_code" => "de",
             "name" => "Berlin",
           ],
         ],
       }
    >>> $grouped = $cities->groupBy('country_code');
    => Illuminate\Support\Collection {#836
         all: [
           "pl" => Illuminate\Support\Collection {#838
             all: [
               [
                 "country_code" => "pl",
                 "name" => "Warszawa",
               ],
               [
                 "country_code" => "pl",
                 "name" => "Wrocław",
               ],
             ],
           },
           "de" => Illuminate\Support\Collection {#837
             all: [
               [
                 "country_code" => "de",
                 "name" => "Berlin",
               ],
             ],
           },
         ],
       }
    >>> foreach ($grouped as $cCode => $cities) {
    ... echo $cCode . ' has '.$cities->count()."
    ";
    ... }
    pl has 2
    de has 1
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致