2012-08-10 55 views
0
Array (
[0] => Array ([SiteID] => 147 [Amount] => 500.00 [TransactionType] => Deposit) 

[1] => Array ([SiteID] => 147 [Amount] => 500.00 [TransactionType] => Redemption) 

[2] => Array ([SiteID] => 147 [Amount] => 1500.00[TransactionType] => Deposit) 

[3] => Array ([SiteID] => 147 [Amount] => 200.00 [TransactionType] => Reload) 

[4] => Array ([SiteID] => 150 [Amount] => 100.00 [TransactionType] => Deposit) 

[5] => Array ([SiteID] => 3 [Amount] => 500.00 [TransactionType] => Redemption) 

[6] => Array ([SiteID] => 150 [Amount] => 200.00 [TransactionType] => Redemption) 

[7] => Array ([SiteID] => 3 [Amount] => 500.00 [TransactionType] => Deposit) 

[8] => Array ([SiteID] => 3 [Amount] => 200.00 [TransactionType] => Deposit) 

[9] => Array ([SiteID] => 3 [Amount] => 200.00 [TransactionType] => Reload) 

[10] => Array ([SiteID] =>147 [Amount] => 500.00 [TransactionType] => Redemption)) 
) 

如何根據相同的'SiteID'和'TransactionType'來總計'金額'。 我的數據將顯示如下:按同一個鍵排列數值總和

array ([147]=>array([Deposit] => "total amount", [Reload]=> "total amount" [Redemption]=> "total amount")) 

array ([150]=>array([Deposit] => "total amount", [Reload]=> "total amount" [Redemption]=> "total amount")) 

array ([3]=>array ([Deposit] => "total amount", [Reload]=> "total amount" [Redemption]=> "total amount")) 

謝謝。請回復。 :)

+0

你是如何得到這個數據?從MySQL? – 2012-08-10 03:57:09

+0

是表單mysql,但函數不需要使用查詢,它應該是一個數組。:) – Happy 2012-08-10 04:48:43

+0

請提供一個工作數組。 – 2012-08-10 05:14:30

回答

2

請在將來提供一個工作陣列片段。

$transactions = array (
    array('SiteID' => 147, 'Amount' => '500.00', 'TransactionType' => 'Deposit'), 
    array('SiteID' => 147, 'Amount' => '500.00', 'TransactionType' => 'Redemption'), 
    array('SiteID' => 147, 'Amount' => '1500.00', 'TransactionType' => 'Deposit'), 
    array('SiteID' => 147, 'Amount' => '200.00', 'TransactionType' => 'Reload'), 
    array('SiteID' => 150, 'Amount' => '100.00', 'TransactionType' => 'Deposit'), 
    array('SiteID' => 3, 'Amount' => '500.00', 'TransactionType' => 'Redemption'), 
    array('SiteID' => 150, 'Amount' => '200.00', 'TransactionType' => 'Redemption'), 
    array('SiteID' => 3, 'Amount' => '500.00', 'TransactionType' => 'Deposit'), 
    array('SiteID' => 3, 'Amount' => '200.00', 'TransactionType' => 'Deposit'), 
    array('SiteID' => 3, 'Amount' => '200.00', 'TransactionType' => 'Reload'), 
    array('SiteID' =>147, 'Amount' => '500.00', 'TransactionType' => 'Redemption') 
); 

$totals = null; 

foreach ($transactions as $t){ 
    $amount = (float) $t['Amount']; 
    if (isset($totals[ $t['SiteID'] ][ $t['TransactionType'] ])){ 
     $totals[ $t['SiteID'] ][ $t['TransactionType'] ] += (float) $amount; 
    } else { 
     $totals[ $t['SiteID'] ][ $t['TransactionType'] ] = (float) $amount; 
    } 
} 

print_r ($totals); 

這會產生這樣的結果:

 
Array 
(
    [147] => Array 
     (
      [Deposit] => 2000 
      [Redemption] => 1000 
      [Reload] => 200 
     ) 

    [150] => Array 
     (
      [Deposit] => 100 
      [Redemption] => 200 
     ) 

    [3] => Array 
     (
      [Redemption] => 500 
      [Deposit] => 700 
      [Reload] => 200 
     ) 

) 

如果你能處理PHP通知警告迴路可以縮短爲:

foreach ($transactions as $t){ 
    $totals[ $t['SiteID'] ][ $t['TransactionType'] ] += (float) $t['Amount']; 
} 
相關問題