欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

数组中对某项相同的值进行求和计算

程序员文章站 2022-05-29 17:44:04
...
已知数组:
$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)  }};


求当fg_type项相同时,针对item1,item2,rs1,rs2项的求和,并将求得的结果放在此数组后。


回复讨论(解决方案)

没人回答吗?

	$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';	print_r($arr);

测试结果应该没问题,不知道这么写好不好,凑合着看...