Reputation: 327
I have an array which contains another array values: Ex.
array:69 [▼
0 => array:9 [▼
"app" => "a.log"
"context" => "local"
"level" => "error"
"level_class" => "danger"
I want to group all the error according to their levels Ex:
array:
"error" => "count of Errors",
"debug" => "count of debug"
I tried doing this:
foreach($logs as $log){
$result[$log['level']] = $log;
}
The result i get is:
array:2 [▼
"error" => "Last error entry in array"
"failed" => "Last failed entry in array"
]
Any help is appreciated. Thank You.
Upvotes: 0
Views: 515
Reputation: 41810
When you do $result[$log['level']] = $log;
, you're replacing the value in $result[$log['level']]
on each iteration, which is why you end up with only the last entry for each level. Instead you need to use
$result[$log['level']][] = $log;
To append to that key instead of replacing it.
After fixing that, you'll have an array of arrays of arrays, instead of just an array of arrays, and you can get the counts with
$counts = array_map('count', $result);
Upvotes: 2