php 中的多维数组总和
Multidimensional array sum in php
我的数组是:
Array
(
[0] => Array
(
[Name] => DCL
[Duration] => 18
[Min] => 31
[CostD] => 4
[CostD1] => 4
)
[1] => Array
(
[Name] => Ins
[Duration] => 51
[Min] => 1
[CostD] => 0.00
[CostD1] => 0.04
)
[2] => Array
(
[Name] => N2P
[Duration] => 51
[Min] => 1
[CostD] => 0.04
[CostD1] => 0
)
[3] => Array
(
[Name] => N2P
[Duration] => 17
[Min] => 2
[CostD] => 13.61
[CostD1] => 13
)
[4] => Array
(
[Name] => DCL
[Duration] => 10
[Min] => 0
[CostD] => 5
[CostD1] => 5
)
[5] => Array
(
[Name] => Ins
[Duration] => 300
[Min] => 5
[CostD] => 0.44
[CostD1] => 0.00
)
[6] => Array
(
[Name] => RNB
[Duration] => 341
[Min] => 0
[CostD] => 0.44
[CostD1] => 0.00
)
[7] => Array
(
[Name] => DCL
[Duration] => 20
[Min] => 0
[CostD] => 12.8
[CostD1] => 12
)
)
我想要像
这样的新数组
Array
(
[0] => Array
(
[Name] => DCL
[Duration] => 48
[Min] => 31
[CostD] => 21.8
[CostD1] => 21
)
[1] => Array
(
[Name] => Ins
[Duration] => 351
[Min] => 1
[CostD] => 0.00
[CostD1] => 0.04
)
[2] => Array
(
[Name] => N2P
[Duration] => 68
[Min] => 3
[CostD] => 13.16
[CostD1] => 13
)
[3] => Array
(
[Name] => RNB
[Duration] => 341
[Min] => 0
[CostD] => 0.44
[CostD1] => 0.00
)
)
请help.Any帮助将不胜感激
$sumArray = array();
foreach ($data as $k => $row) {
if (!isset($sumArray[$row['Name']])) {
$sumArray[$row['Name']] = $row;
} else {
foreach ($row as $k => $v) {
if (is_numeric($v)) {
$sumArray[$row['Name']][$k]+=$v;
} else {
// decide here what to do if value is a string
}
}
}
}
$result = array_values($sumArray);
foreach($data as $r){
$values[$r['Name']]['Name'] = $r['Name'];
$values[$r['Name']]['Duration'] += $r['Duration'];
$values[$r['Name']]['Min'] += $r['Min'];
$values[$r['Name']]['CostD'] += $r['CostD'];
$values[$r['Name']]['CostD1'] += $r['CostD1'];
}
print_r(array_values($values));
我的数组是:
Array
(
[0] => Array
(
[Name] => DCL
[Duration] => 18
[Min] => 31
[CostD] => 4
[CostD1] => 4
)
[1] => Array
(
[Name] => Ins
[Duration] => 51
[Min] => 1
[CostD] => 0.00
[CostD1] => 0.04
)
[2] => Array
(
[Name] => N2P
[Duration] => 51
[Min] => 1
[CostD] => 0.04
[CostD1] => 0
)
[3] => Array
(
[Name] => N2P
[Duration] => 17
[Min] => 2
[CostD] => 13.61
[CostD1] => 13
)
[4] => Array
(
[Name] => DCL
[Duration] => 10
[Min] => 0
[CostD] => 5
[CostD1] => 5
)
[5] => Array
(
[Name] => Ins
[Duration] => 300
[Min] => 5
[CostD] => 0.44
[CostD1] => 0.00
)
[6] => Array
(
[Name] => RNB
[Duration] => 341
[Min] => 0
[CostD] => 0.44
[CostD1] => 0.00
)
[7] => Array
(
[Name] => DCL
[Duration] => 20
[Min] => 0
[CostD] => 12.8
[CostD1] => 12
)
)
我想要像
这样的新数组Array
(
[0] => Array
(
[Name] => DCL
[Duration] => 48
[Min] => 31
[CostD] => 21.8
[CostD1] => 21
)
[1] => Array
(
[Name] => Ins
[Duration] => 351
[Min] => 1
[CostD] => 0.00
[CostD1] => 0.04
)
[2] => Array
(
[Name] => N2P
[Duration] => 68
[Min] => 3
[CostD] => 13.16
[CostD1] => 13
)
[3] => Array
(
[Name] => RNB
[Duration] => 341
[Min] => 0
[CostD] => 0.44
[CostD1] => 0.00
)
)
请help.Any帮助将不胜感激
$sumArray = array();
foreach ($data as $k => $row) {
if (!isset($sumArray[$row['Name']])) {
$sumArray[$row['Name']] = $row;
} else {
foreach ($row as $k => $v) {
if (is_numeric($v)) {
$sumArray[$row['Name']][$k]+=$v;
} else {
// decide here what to do if value is a string
}
}
}
}
$result = array_values($sumArray);
foreach($data as $r){
$values[$r['Name']]['Name'] = $r['Name'];
$values[$r['Name']]['Duration'] += $r['Duration'];
$values[$r['Name']]['Min'] += $r['Min'];
$values[$r['Name']]['CostD'] += $r['CostD'];
$values[$r['Name']]['CostD1'] += $r['CostD1'];
}
print_r(array_values($values));