2017-07-07 64 views
0

對不起,我看到多個類似的解決方案,但我無法在我的情況下實施它們。我試圖檢查是否存在價值之前存在價值,但它給我真正的價值,不能夠增加其實際價值。使用波紋管代碼,只要循環運行並給出錯誤的總和值,它就會重複相同的值。請糾正我。如何通過圖表的鍵爲數組值進行求和?

我想通過匹配鍵數組值添加到單個陣列..

我有這個陣列...

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']; 

     } 
+1

什麼是你的電流輸出? –

+0

我不明白你的問題,因爲你的代碼完全符合你的需求:http://sandbox.onlinephpfunctions.com/code/9e7f683636fcd417059e066326509addad77dc73](http://sandbox.onlinephpfunctions.com/code/9e7f683636fcd417059e066326509addad77dc73),可能是這是通知? – Jimbot

+0

它給了我在這個poing正確的價值,但是當上傳這個代碼到活動服務器,其中n數是陣列它添加最後一個值,直到foreach結束他們的工作。 – SAHAR

回答

0

您的代碼看起來不錯,你只需要檢查,如果數組元素存在

foreach($records as $y_key => $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'] += isset($y_value['Customer']['amount']) ? $y_value['Customer']['amount'] : 0; 
      $top_performer[$y_value['Customer']['created_by']]['refund'] += isset($y_value['Customer']['refund_amount']) ? $y_value['Customer']['refund_amount'] : 0; 
      $top_performer[$y_value['Customer']['created_by']]['upgrade'] += isset($y_value['Customer']['upgrade_amount']) ? $y_value['Customer']['upgrade_amount'] : 0; 
    } 
+0

用PHP7,可以縮寫'isset($ y_value ['Customer'] ['amount'])? $ y_value ['Customer'] ['amount']:0' to'$ y_value ['Customer'] ['amount'] ?? 0' –

+0

感謝@ivan Bolnikh的幫助。我已經嘗試過這種解決方案,但在活服務器上仍然出現錯誤結果。在得到相同的答案後,我嘗試瞭解我的其他問題,然後再次檢查我的代碼。我發現我的主數組有重複的數據,這就是爲什麼我得到錯誤的結果。謝謝你的幫助。 – SAHAR

+0

對不起@尼格爾,我仍然使用舊的PHP 5.6 :) – SAHAR

相關問題