adding array values
- by christian
Array
(
[0] => Array
(
[datas] => Array
(
[name] => lorem
[id] => 1
[type] => t1
[due_type] => Q1
[t1] => 1
[t2] => 1
[t3] => 1
)
)
[1] => Array
(
[datas] => Array
(
[name] => lorem
[id] => 1
[type] => t2
[due_type] => Q1
[t1] => 0
[t2] => 1
[t3] => 0
)
)
[2] => Array
(
[datas] => Array
(
[name] => name
[id] => 2
[type] => t1
[due_type] => Q1
[t1] => 1
[t2] => 0
[t3] => 1
)
)
[3] => Array
(
[datas] => Array
(
[name] => name
[id] => 2
[type] => t2
[due_type] => Q1
[t1] => 1
[t2] => 0
[t3] => 0
)
)
)
I want to add the values of each array according to its id, but I am having problem getting the values using these code: I want to compute the sum of all type according to each due_type and combining them into one array.
$totals = array();
$i = -1;
foreach($datas as $key => $row){
$i += 1;
$items[$i] = $row;
if (isset($totals[$items[$i]['datas']['id']])){
if($totals[$items[$i]['datas']['id']]['due_type'] == 'Q1'){
if($totals[$items[$i]['datas']['id']]['type'] == 't1'){
$t1+=$totals[$items[$i]['datas']['id']]['t1'];
}elseif($totals[$items[$i]['datas']['id']]['type'] == 't2'){
$t2+=$totals[$items[$i]['datas']['id']]['t2'];
}elseif($totals[$items[$i]['datas']['id']]['type'] == 't3'){
$t3+=$totals[$items[$i]['datas']['id']]['t3'];
}
$totals[$items[$i]['datas']['id']]['t1_total'] = $t1;
$totals[$items[$i]['datas']['id']]['t2_total'] = $t2;
}
}
else {
$totals[$items[$i]['datas']['id']] = $row['datas'];
$totals[$items[$i]['datas']['id']]['t1_total'] = $items[$i]['datas']['t1'];
$totals[$items[$i]['datas']['id']]['t2_total'] = $items[$i]['datas']['t2'];
}
}