本文介绍了阵列印刷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这里的code,但它打印的是这样的:

Here's the code but it print's like this:

Array ( [147] => Array ( [D] => 455500 [W] => 460216.4 [R] => 158000 ) 

[145] => Array ( [D] => 85500 [W] => 78763.75 [R] => 14500 )

[146] => Array ( [D] => 64200 [W] => 91121.94 [R] => 42800 )

我需要的print_r的结果是这样的:

I need to print_r the result like this:

Array ( [147] => Array ( [Deposit] => 455500 [Redemption] => 460216.4 [Reload] => 158000 ) 

[145] => Array ( [Deposit] => 85500 [Redemption] => 78763.75 [Reload] => 14500 )

[146] => Array ( [Deposit] => 64200 [Redemption] => 91121.94 [Reload] => 42800 )

的R - 刷新,W - 赎回和D - 存款

感谢,并请响应。

推荐答案

尝试

foreach ($transactions as $k=>$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;
}
}

这篇关于阵列印刷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 18:38