dongtangjie0495 2014-06-14 18:12
浏览 28
已采纳

php - 为foreach循环中的客户端列表添加总计

I'm looping through a list of client names to get their total minutes used for a service. The problem is, each client has more than one database so I have to loop through those too, and then add the totals associated with each client. I'm not able to add the results for each client. What am I doing wrong?

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>";
    }
}

Output

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 to get client totals (feeble attempt):

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

The results from this are incorrect.

  • 写回答

1条回答 默认 最新

  • duannei1477 2014-06-14 18:26
    关注

    If i get the question correctly, each client accesses in different times and put in different sources. Well in that case u can use a unique field of the client (something like the email) to build another.

    $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>";
      }
    }
    

    then u can print $res here!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大