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. :)
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.
Something like
print_r(array_count_values(array_column($array, 'app')));
I would test it but you didn't provide an array i can use. In theory it should work.