2012-08-10 257 views
0
$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 ([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的結果是這樣的:

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) 

研究 - 刷新,W - 贖回及D - 存款。

謝謝並請回覆。

+0

感謝diEcho,你能幫助我與我的問題?謝謝:) – Happy 2012-08-10 05:51:45

+0

我無法找到你想要的數組中的變化,目前正在獲取數組。除了'D W R',但後來你寫了D W R的縮寫 – diEcho 2012-08-10 05:53:45

+0

兩個數組之間有什麼區別?你到底想要什麼? – WatsMyName 2012-08-10 05:55:05

回答

0

使用

$result=array(); 
foreach($total as $key=>$value) 
{ 
    $result=array_merge($result,array($key=>array("Deposit"=>$value['D'],"Redemption"=>$value['W'],"Reload"=>$value['R']))); 
} 
print_r($result); 
+0

嗨,但我需要在密鑰處插入SiteID?我該怎麼做? – Happy 2012-08-10 07:53:55

+0

siteid是$ key。 – 2012-08-10 08:06:30

+0

我該怎麼做?:( – Happy 2012-08-10 08:08:38

0

TRY

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; 
} 
} 
+0

僅在'foreach'中更改 – diEcho 2012-08-10 06:03:46