3v4l.org

run code in 300+ PHP versions simultaneously
<?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);
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.28, 8.4.1 - 8.4.14, 8.5.0 - 8.5.1
array(3) { ["bhavin"]=> array(3) { ["duration"]=> float(1197.1299999999999) ["open"]=> int(1) ["closed"]=> int(1) } ["lokesh"]=> array(3) { ["duration"]=> float(132.482) ["open"]=> NULL ["closed"]=> int(2) } ["total"]=> array(3) { ["duration"]=> float(1329.6119999999999) ["open"]=> int(1) ["closed"]=> int(3) } }
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.
Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
array(3) { ["bhavin"]=> array(3) { ["duration"]=> float(1197.13) ["open"]=> int(1) ["closed"]=> int(1) } ["lokesh"]=> array(3) { ["duration"]=> float(132.482) ["open"]=> NULL ["closed"]=> int(2) } ["total"]=> array(3) { ["duration"]=> float(1329.612) ["open"]=> int(1) ["closed"]=> int(3) } }

preferences:
191.12 ms | 411 KiB | 5 Q