在foreach循环中为客户端列表添加总数


adding totals for list of clients in foreach loop

我正在遍历客户端名称列表以获取他们用于服务的总分钟数。问题是,每个客户端都有多个数据库,因此我也必须遍历这些数据库,然后添加与每个客户端关联的总数。我无法为每个客户添加结果。我做错了什么?

PHP

foreach ($clients as $client) {
    foreach ($dbNew as $d) {
        $result += (strtotime($closing_time[$x]->log_time) - strtotime($opening_time[$x]->log_time));
        print "<pre>";
        print_r($client);
        print " --- ";
        print_r($result);
        print "</pre>";
    }
}

输出
adarty@cfl_rr.com --- 425
adarty@cfl_rr.com --- 225
doug --- 0
doug --- 0
rforgo@gmail.com --- 0
rforgo@gmail.com --- 4357
rforgo@gmail.com --- 336

PHP获取客户端总数:

$client_result = 0;
if($client == $client)
$client_result = $result + $result;

结果是不正确的

如果我得到正确的问题,每个客户端访问在不同的时间和不同的来源。在这种情况下,你可以使用客户端的唯一字段(比如电子邮件)来构建另一个。

$res = array();
foreach ($clients as $client) {
    foreach ($dbNew as $d) {
       $result += (strtotime($closing_time[$x]->log_time) - strtotime($opening_time[$x]->log_time));
        print "<pre>";
        print_r($client);
        if(isset($res[$client[0]])){
       //i guessed the first field of $client is the email
       //if the client is already added to the $res list add the current time to it
            $res[$client[0]] += $result;
        }else{
        //else this is the first instance of the client....
            $res[$client[0]] = $result;
        }
        print " --- ";
        print_r($result);
        print "</pre>";
  }
}

那么你可以在这里打印$res !