string(4) ""/> string(4) "">
数组中对某项相同的值进行求和计算
已知数组:
$arr = array(15) {<br /> [0]=><br /> array(9) {<br /> ["lppt"]=><br /> string(4) "LP1 "<br /> ["fg_type"]=><br /> string(5) "ECU "<br /> ["customer_no"]=><br /> string(4) "A001"<br /> ["etype1"]=><br /> string(3) "DIG"<br /> ["RevDegree"]=><br /> string(2) "S "<br /> ["item1"]=><br /> float(8810)<br /> ["rs1"]=><br /> float(5787788.527)<br /> ["item2"]=><br /> float(8928)<br /> ["rs2"]=><br /> float(5865309.4176)<br /> }<br /> [1]=><br /> array(9) {<br /> ["lppt"]=><br /> string(4) "LP1 "<br /> ["fg_type"]=><br /> string(3) "ECU"<br /> ["customer_no"]=><br /> string(4) "A001"<br /> ["etype1"]=><br /> string(3) "MPI"<br /> ["RevDegree"]=><br /> string(1) "B"<br /> ["item1"]=><br /> float(0)<br /> ["rs1"]=><br /> float(0)<br /> ["item2"]=><br /> float(0)<br /> ["rs2"]=><br /> float(0)<br /> }<br /> [2]=><br /> array(9) {<br /> ["lppt"]=><br /> string(4) "LP1 "<br /> ["fg_type"]=><br /> string(4) "ECU "<br /> ["customer_no"]=><br /> string(4) "A001"<br /> ["etype1"]=><br /> string(3) "MPI"<br /> ["RevDegree"]=><br /> string(2) "S "<br /> ["item1"]=><br /> float(63485)<br /> ["rs1"]=><br /> float(23211270.7503)<br /> ["item2"]=><br /> float(71973)<br /> ["rs2"]=><br /> float(26689668.0654)<br /> }<br /> [3]=><br /> array(9) {<br /> ["lppt"]=><br /> string(4) "LP1 "<br /> ["fg_type"]=><br /> string(4) "ECU "<br /> ["customer_no"]=><br /> string(4) "A002"<br /> ["etype1"]=><br /> string(3) "MPI"<br /> ["RevDegree"]=><br /> string(2) "S "<br /> ["item1"]=><br /> float(4810)<br /> ["rs1"]=><br /> float(2329072.60146)<br /> ["item2"]=><br /> float(5855)<br /> ["rs2"]=><br /> float(2783605.29921)<br /> }<br /> [4]=><br /> array(9) {<br /> ["lppt"]=><br /> string(4) "LP1 "<br /> ["fg_type"]=><br /> string(5) "ECU "<br /> ["customer_no"]=><br /> string(4) "A004"<br /> ["etype1"]=><br /> string(3) "MPI"<br /> ["RevDegree"]=><br /> string(2) "S "<br /> ["item1"]=><br /> float(1716)<br /> ["rs1"]=><br /> float(652004.847)<br /> ["item2"]=><br /> float(1711)<br /> ["rs2"]=><br /> float(647801.6757)<br /> }<br />[5]=><br /> array(9) {<br /> ["lppt"]=><br /> string(4) "LP2 "<br /> ["fg_type"]=><br /> string(3) "ECU"<br /> ["customer_no"]=><br /> string(4) "A001"<br /> ["etype1"]=><br /> string(3) "MPI"<br /> ["RevDegree"]=><br /> string(1) "B"<br /> ["item1"]=><br /> float(0)<br /> ["rs1"]=><br /> float(0)<br /> ["item2"]=><br /> float(0)<br /> ["rs2"]=><br /> float(0)<br /> }<br />};
$arr = array(<br /> array('fg_type'=>'ECU1', 'item1'=>8810, 'rs1'=>5787788.527, 'item2'=>8928, 'rs2'=>5865309.4176),<br /> array('fg_type'=>'ECU1', 'item1'=>0, 'rs1'=>0, 'item2'=>0, 'rs2'=>0),<br /> array('fg_type'=>'ECU2', 'item1'=>63485, 'rs1'=>23211270.7503, 'item2'=>71973, 'rs2'=>26689668.0654),<br /> array('fg_type'=>'ECU2', 'item1'=>4810, 'rs1'=>2329072.60146, 'item2'=>5855, 'rs2'=>2783605.29921),<br /> array('fg_type'=>'ECU3', 'item1'=>1716, 'rs1'=>652004.847, 'item2'=>1711, 'rs2'=>647801.6757),<br /> array('fg_type'=>'ECU3', 'item1'=>0, 'rs1'=>0, 'item2'=>0, 'rs2'=>0),<br /> );<br /> <br /> $new_arr = array();<br /> for($i=0; $i<count($arr); $i++){<br /> $arr[$i]['sum'] = $arr[$i]['item1'] + $arr[$i]['rs1'] + $arr[$i]['item2'] + $arr[$i]['rs2'];<br /> $new_arr[$arr[$i]['fg_type']] = 0;<br /> }<br /> <br /> for($i=0; $i<count($arr); $i++){<br /> if(array_key_exists($arr[$i]['fg_type'], $new_arr)){<br /> $new_arr[$arr[$i]['fg_type']] += $arr[$i]['sum'];<br /> }<br /> }<br /> <br /> array_push($arr,$new_arr);<br /> <br /> echo '<pre class="brush:php;toolbar:false">';<br /> print_r($arr);