简体   繁体   English

由同一个键组成的数组总和

[英]Array sum of value by same key

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

How to sum 'Amount' based on same 'SiteID' and 'TransactionType'. 如何根据相同的“SiteID”和“TransactionType”对“金额”求和。 My data will shown like this: 我的数据将显示如下:

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

Thanks. 谢谢。 Please Response. 请回复。 :) :)

please provide a working array fragment in future. 请在将来提供一个工作数组片段。

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

This will produce a result like this: 这将产生如下结果:

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

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

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

)

If you can deal with php notice warnings the loop can be shortened to: 如果你可以处理php通知警告,循环可以缩短为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM