數組中對某項雷同的值進行求和計算

來源:互聯網
上載者:User
數組中對某項相同的值進行求和計算
已知數組:
$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$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; $iif(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);

測試結果應該沒問題,不知道這麼寫好不好,湊合著看...
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.