我在codeigniter项目的控制器中有以下代码:
foreach($attendace as $k => $v) {
$id = $v['project_id'];
$result[$id]['project_name'] = $v['project_name'];
$result[$id]['labour'] += $v['no_of_labours'];
$result[$id]['total_time'] += ($v['stop_time']-$v['start_time'])*$v['no_of_labours'];
}
此代码提供一个错误未知变量人工和总时间。如果我尝试初始化这些变量如下:
foreach($attendace as $k => $v) {
$id = $v['project_id'];
$result[$id]['labour'] =0;
$result[$id]['total_time']=0;
$result[$id]['project_name'] = $v['project_name'];
$result[$id]['labour'] += $v['no_of_labours'];
$result[$id]['total_time'] += ($v['stop_time']-$v['start_time'])*$v['no_of_labours'];
}
添加$result[$id]['labour']=0;和$result[$id]['total_time']=0;则我得到的附加值是错误的。基本上不会增加。
因此,谁能告诉我如何解决这个问题?