<?php $arr = Array( "0" => Array ( "id" => "pZgauDZtvQ9grRD9c", "rid" => "obHEGwnrfHKCd32jF", "starttime" => "14-02-2018", "chatduration" => 124.502, "username" => "bhavin", "status" => "open" ), "1" => Array ( "id" => "ej5WfYe3dn8mtHzQF", "rid" => "o6pKCt4e6RaYiZhQ8", "starttime" => "14-02-2018", "chatduration" => 1072.628, "username" => "bhavin", "status" => "closed" ), "2" => Array ( "id" => "qCzZjf7vb4rSBRndk", "rid" => "ZFvuuFiL9RFiemX3K", "starttime" => "14-02-2018", "chatduration" => 11.254, "username" => "lokesh", "status" => "closed" ), "3" => Array ( "id" => "TCdSycFFSeY8WaKzG", "rid" => "f7ADPWFsCD6RZFspk", "starttime" => "14-02-2018", "chatduration" => 121.228, "username" => "lokesh", "status" => "closed" ) ); // create new associative array foreach($arr as $subarr){ $new[$subarr["username"]][] = $subarr; } // loop each username and create result array foreach($new as $user => $subarr){ $result[$user]["duration"] = array_sum(array_column($subarr, "chatduration")); // @ to supress notice if no open/closed is in array @$result[$user]["open"] = array_count_values(array_column($subarr, "status"))["open"]; @$result[$user]["closed"] = array_count_values(array_column($subarr, "status"))["closed"]; } $result["total"]["duration"] = array_sum(array_column($result, "duration")); $result["total"]["open"] = array_sum(array_column($result, "open")); $result["total"]["closed"] = array_sum(array_column($result, "closed")); var_dump($result);
You have javascript disabled. You will not be able to edit any code.