0

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.

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.