對不起,我看到多個類似的解決方案,但我無法在我的情況下實施它們。我試圖檢查是否存在價值之前存在價值,但它給我真正的價值,不能夠增加其實際價值。使用波紋管代碼,只要循環運行並給出錯誤的總和值,它就會重複相同的值。請糾正我。如何通過圖表的鍵爲數組值進行求和?
我想通過匹配鍵數組值添加到單個陣列..
我有這個陣列...
Array(
[0] => Array
(
[Customer] => Array
(
[created_by] => 3
[amount] => 475
)
)
[1] => Array
(
[Customer] => Array
(
[created_by] => 5
[amount] => 199
)
)
[2] => Array
(
[Customer] => Array
(
[created_by] => 1
[amount] => 199
[refund_amount] => 200
[upgrade_amount] => 199
)
)
[3] => Array
(
[Customer] => Array
(
[created_by] => 1
[amount] => 199
)
)
[4] => Array
(
[Customer] => Array
(
[created_by] => 4
[upgrade_amount] => 199
)
)
) 我正在尋找這樣的結果.... 。
Array(
[3] => Array
(
[sale] => 475
[refund] => 0
[upgrade] => 0
)
[5] => Array
(
[sale] => 199
[refund] => 0
[upgrade] => 0
)
[1] => Array
(
[sale] => 398
[refund] => 200
[upgrade] => 199
)
[4] => Array
(
[sale] => 0
[refund] => 0
[upgrade] => 199
)
)
我使用這個代碼讓我的結果....
foreach($records as $y_key => $y_value){
//print_r($y_value);
if(!isset($top_performer[$y_value['Customer']['created_by']])){
$top_performer[$y_value['Customer']['created_by']]['sale'] = 0;
$top_performer[$y_value['Customer']['created_by']]['refund'] = 0;
$top_performer[$y_value['Customer']['created_by']]['upgrade'] = 0;
}
$top_performer[$y_value['Customer']['created_by']]['sale']+= $y_value['Customer']['amount'];
$top_performer[$y_value['Customer']['created_by']]['refund']+= $y_value['Customer']['refund_amount'];
$top_performer[$y_value['Customer']['created_by']]['upgrade']+=$y_value['Customer']['upgrade_amount'];
}
什麼是你的電流輸出? –
我不明白你的問題,因爲你的代碼完全符合你的需求:http://sandbox.onlinephpfunctions.com/code/9e7f683636fcd417059e066326509addad77dc73](http://sandbox.onlinephpfunctions.com/code/9e7f683636fcd417059e066326509addad77dc73),可能是這是通知? – Jimbot
它給了我在這個poing正確的價值,但是當上傳這個代碼到活動服務器,其中n數是陣列它添加最後一個值,直到foreach結束他們的工作。 – SAHAR