我有一个像这样的数组。
Array
(
[162 D Dr.] => Array
(
[smi_discover] => 0
[smi_totalmonthccsales] => 12568
[smi_amex] => 0
[smi_visamc] => 12568
[smi_ebt] => 0
[smi_includeindeal] => 0
[smi_debit] => 0
[smi_batches] => 7
)
[16ee22 D Dr.] => Array
(
[smi_discover] => 0
[smi_totalmonthccsales] => 125368
[smi_amex] => 0
[smi_visamc] => 12568
[smi_ebt] => 0
[smi_includeindeal] => 0
[smi_debit] => 0
[smi_batches] => 7
)
)
这个钥匙[162 D Dr.]可以是任何东西。 [16e2 D Dree。],[332 D44 Drdds]。
如何循环使用并添加smi_totalmonthccsales并将其添加到父数组中,以便我最终得到类似的内容
['total'] = sum of smi_totalmonthccsales in both address arrays
[162 D Dr.] => Array
(
[smi_discover] => 0
[smi_totalmonthccsales] => 12568
[smi_amex] => 0
[smi_visamc] => 12568
[smi_ebt] => 0
[smi_includeindeal] => 0
[smi_debit] => 0
[smi_batches] => 7
)
我试过了
foreach($newnewArr as $key) {
if(isset($key['smi_totalmonthccsales']) && isset($key)){
$ccArray[$key]['total'] +=$key['smi_totalmonthccsales'];
} else {
$ccArray[$key]['total'] = 0;
}
}
答案 0 :(得分:2)
之后再设置它。
$total = 0;
foreach($arr as $key => $value) {
$total += $value['smi_totalmonthccsales'];
}
$arr['total'] = $total;
你想要一个总计吗?好...
$total = 0;
foreach($arr as &$value) {
$total += $value['smi_totalmonthccsales'];
$value['total'] = $total;
}