多维数组中的用户计数

I have array look like below. And I want to do sum of chatduration for userwise.

And also want to count below 3 detail from status

  1. close_chat
  2. open_chat
  3. total chat.

    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
                )
    
        )
    

    And from this array I want to take sum chatduration userwise. Sum of bhavin user and lokesh user should be store in array.

How can this possible?

You can sum the chat duration like this:

$duration = array_sum(array_column($userArray,'chatduration'));

You can get your counts like this:

$counts = array_count_values(array_column($userArray,'status'));

This last one will return an array using the values of array as keys and their frequency in array as values.

Like this?

$array = unserialize( 'a:4:{i:0;a:6:{s:2:"id";s:17:"pZgauDZtvQ9grRD9c";s:3:"rid";s:17:"obHEGwnrfHKCd32jF";s:9:"starttime";s:10:"14-02-2018";s:12:"chatduration";s:7:"124.502";s:8:"username";s:6:"bhavin";s:6:"status";s:4:"open";}i:1;a:6:{s:2:"id";s:17:"ej5WfYe3dn8mtHzQF";s:3:"rid";s:17:"o6pKCt4e6RaYiZhQ8";s:9:"starttime";s:10:"14-02-2018";s:12:"chatduration";s:8:"1072.628";s:8:"username";s:6:"bhavin";s:6:"status";s:6:"closed";}i:2;a:6:{s:2:"id";s:17:"qCzZjf7vb4rSBRndk";s:3:"rid";s:17:"ZFvuuFiL9RFiemX3K";s:9:"starttime";s:10:"14-02-2018";s:12:"chatduration";s:6:"11.254";s:8:"username";s:6:"lokesh";s:6:"status";s:6:"closed";}i:3;a:6:{s:2:"id";s:17:"TCdSycFFSeY8WaKzG";s:3:"rid";s:17:"f7ADPWFsCD6RZFspk";s:9:"starttime";s:10:"14-02-2018";s:12:"chatduration";s:7:"121.228";s:8:"username";s:6:"lokesh";s:6:"status";s:6:"closed";}}' );


$new_array['open_chats'] = $new_array['closed_chats'] = $new_array['total_chats'] = 0;

foreach( $array as $chat ){

    if( $chat['status']  === 'open')

        $new_array['open_chats']++;

    if( $chat['status'] === 'closed' )

        $new_array['closed_chats']++;


    @$new_array['chat_duration_by_user'][ $chat['username'] ] += $chat['chatduration'];

    $new_array['total_chats']++;

}


print_r( $new_array );

Output

Array
(
    [total_chats] => 4
    [closed_chats] => 3
    [open_chats] => 1
    [chat_duration_by_user] => Array
        (
            [bhavin] => 1197.13
            [lokesh] => 132.482
        )

)

Demo

I have wrote an algorithm which calculates total chat duration user wise, please see demo, here is the code

$user = [];

foreach($arr as $record){

    if (array_key_exists($record['username'],$user))
  {
        $user[$record['username']] = $user[$record['username']] + $record['chatduration'];
        echo "inside";
  }
else
  {
      $user[$record['username']]  = $record['chatduration'];
  }
}

Output:

Array(
[bhavin] => 249.004
 )

You can use array_column, array_count_values and array_sum in a foreach loop like this to get an result array which I think is easy to use.

Notice how i first make an associative array of your input array to make it easier to create the result array.
The associative array means I will get [user][index][status] for example.
That way I can easily use array_sum and array_count_values to get what I need.

$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);

https://3v4l.org/UJn3G

Output of this:

array(3) {
      ["bhavin"]=> array(3) {
            ["duration"]=> 1197.13
            ["open"]=> 1
            ["closed"]=> 1
      }
      ["lokesh"]=> array(3) {
            ["duration"]=> 132.482
            ["open"]=> NULL
            ["closed"]=> 2
      }
      ["total"]=> array(3) {
            ["duration"]=> 1329.612
            ["open"]=> 1
            ["closed"] => 3
      }
}