dongmin4990 2018-10-26 23:50
浏览 88

数组group by和count php

I have 2 log files which i read and get the contents as array. The array which i get is like this:

2 => array:9 [▼
"app" => "a.log"
"context" => "local"
"level" => "error"
"level_class" => "danger"
"level_img" => "exclamation-triangle"
"date" => "2018-10-25 21:01:04"
"text" => "Class 'Arcanedev\Support\Collection' not found 
]
3 => array:9 [▼
"app" => "b.log"
"context" => "local"
"level" => "error"
"level_class" => "danger"
"level_img" => "exclamation-triangle"
"date" => "2018-10-26 16:49:07"

I need to get the count of all the error levels for each file. For example:

a.log => "count of error level for a.log",
b.log => "count of error levels for b.log"

I have tried:

foreach($names as $filename){
            $logs = $this->getLogs($this->filepath.$filename);                
            $result = array();
            $result[ $filename ] = $logs;                
            foreach($logs as $log){
                //$result[ $log['level'] ][] = $log;
                $result[ $filename ][] = $log;
                $counts = array_map('count', $result);
            }
        }

which gives me this result:

array:1 [▼
   "a.log" => 20
]

Any help is appreciated. Thank you in advance. :)

  • 写回答

2条回答 默认 最新

  • doushou5761 2018-10-26 23:58
    关注

    i solved the issue myself. I was tried another approach and it seemed to work. Here is the solution:

    foreach($names as $filename){
       $logs = $this->getLogs($this->filepath.$filename);                
       $result = array();                
       foreach($logs as $log){                    
          $result[ $log['app'] ][] = $log;
          $counts = array_map('count', $result);
       }
    }
    

    and the result:

    array:2 [▼
      "b-laravel.log" => 3
      "laravel.log" => 7
     ]
    

    Thank You.

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘