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"))

谢谢。请回复。:)

4

1 回答 1

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);

这将产生如下结果:

大批
(
    [147] => 数组
        (
            [存款] => 2000
            [赎回] => 1000
            [重新加载] => 200
        )

    [150] => 数组
        (
            [存款] => 100
            [兑换] => 200
        )

    [3] => 数组
        (
            [兑换] => 500
            [存款] => 700
            [重新加载] => 200
        )

)

如果您可以处理 php 通知警告,则可以将循环缩短为:

foreach ($transactions as $t){
    $totals[ $t['SiteID'] ][ $t['TransactionType'] ] += (float) $t['Amount'];
}
于 2012-08-10T05:04:33.337 回答