Sums the same values for an item in an array
Known quantity group:
$arr = Array (15) {
[0]=>
Array (9) {
["Lppt"]=>
String (4) "LP1"
["Fg_type"]=>
String (5) "ECU"
["Customer_no"]=>
String (4) "A001"
["Etype1"]=>
String (3) "DIG"
["Revdegree"]=>
String (2) "S"
["Item1"]=>
Float (8810)
["Rs1"]=>
Float (5787788.527)
["Item2"]=>
Float (8928)
["Rs2"]=>
Float (5865309.4176)
}
[1]=>
Array (9) {
["Lppt"]=>
String (4) "LP1"
["Fg_type"]=>
String (3) "ECU"
["Customer_no"]=>
String (4) "A001"
["Etype1"]=>
String (3) "MPI"
["Revdegree"]=>
String (1) "B"
["Item1"]=>
Float (0)
["Rs1"]=>
Float (0)
["Item2"]=>
Float (0)
["Rs2"]=>
Float (0)
}
[2]=>
Array (9) {
["Lppt"]=>
String (4) "LP1"
["Fg_type"]=>
String (4) "ECU"
["Customer_no"]=>
String (4) "A001"
["Etype1"]=>
String (3) "MPI"
["Revdegree"]=>
String (2) "S"
["Item1"]=>
Float (63485)
["Rs1"]=>
Float (23211270.7503)
["Item2"]=>
Float (71973)
["Rs2"]=>
Float (26689668.0654)
}
[3]=>
Array (9) {
["Lppt"]=>
String (4) "LP1"
["Fg_type"]=>
String (4) "ECU"
["Customer_no"]=>
String (4) "A002"
["Etype1"]=>
String (3) "MPI"
["Revdegree"]=>
String (2) "S"
["Item1"]=>
Float (4810)
["Rs1"]=>
Float (2329072.60146)
["Item2"]=>
Float (5855)
["Rs2"]=>
Float (2783605.29921)
}
[4]=>
Array (9) {
["Lppt"]=>
String (4) "LP1"
["Fg_type"]=>
String (5) "ECU"
["Customer_no"]=>
String (4) "A004"
["Etype1"]=>
String (3) "MPI"
["Revdegree"]=>
String (2) "S"
["Item1"]=>
Float (1716)
["Rs1"]=>
Float (652004.847)
["Item2"]=>
Float (1711)
["Rs2"]=>
Float (647801.6757)
}
[5]=>
Array (9) {
["Lppt"]=>
String (4) "LP2"
["Fg_type"]=>
String (3) "ECU"
["Customer_no"]=>
String (4) "A001"
["Etype1"]=>
String (3) "MPI"
["Revdegree"]=>
String (1) "B"
["Item1"]=>
Float (0)
["Rs1"]=>
Float (0)
["Item2"]=>
Float (0)
["Rs2"]=>
Float (0)
}
};
The sum of the item1,item2,rs1,rs2 items is fg_type, and the result of the evaluation is placed after this array.
------to solve the idea----------------------
$arr = Array (
Array (' fg_type ' = ' ECU1 ', ' item1 ' =>8810, ' rs1 ' =>5787788.527, ' item2 ' =>8928, ' rs2 ' =>5865309.4176),
Array (' fg_type ' = ' ECU1 ', ' item1 ' =>0, ' rs1 ' =>0, ' item2 ' =>0, ' rs2 ' =>0),
Array (' fg_type ' = ' ECU2 ', ' item1 ' =>63485, ' rs1 ' =>23211270.7503, ' item2 ' =>71973, ' rs2 ' =>26689668.0654 ),
Array (' fg_type ' = ' ECU2 ', ' item1 ' =>4810, ' rs1 ' =>2329072.60146, ' item2 ' =>5855, ' rs2 ' =>2783605.29921),
Array (' fg_type ' = ' ECU3 ', ' item1 ' =>1716, ' rs1 ' =>652004.847, ' item2 ' =>1711, ' rs2 ' =>647801.6757),
Array (' fg_type ' = ' ECU3 ', ' item1 ' =>0, ' rs1 ' =>0, ' item2 ' =>0, ' rs2 ' =>0),
);
$new _arr = Array ();
for ($i =0; $i
$arr [$i] [' sum '] = $arr [$i] [' item1 '] + $arr [$i] [' rs1 '] + $arr [$i] [' item2 '] + $arr [$i] [' rs2 '];
$new _arr[$arr [$i] [' fg_type ']] = 0;
}
for ($i =0; $i
if (Array_key_exists ($arr [$i] [' Fg_type '], $new _arr)) {
$new _arr[$arr [$i] [' fg_type ']] + = $arr [$i] [' Sum '];
}
}
Array_push ($arr, $new _arr);
Echo '';
Print_r ($arr);
Test results should be no problem, do not know how to write well, do not make a look ...