2012-08-09 102 views
0

要清楚,這裏的輸入數據:如何總結數組值?

陣列 ( [0] =>數組 ( [SITEID] => 147 [量] => 500.00 [TRANSACTIONTYPE] => d )

[1] => Array 
    (
     [SiteID] => 145 
     [Amount] => 500.00 
     [TransactionType] => D 
    ) 

[2] => Array 
    (
     [SiteID] => 145 
     [Amount] => 500.00 
     [TransactionType] => D 
    ) 

[3] => Array 
    (
     [SiteID] => 147 
     [Amount] => 500.00 
     [TransactionType] => D 
    ) 

[4] => Array 
    (
     [SiteID] => 146 
     [Amount] => 500.00 
     [TransactionType] => D 
    ) 
[5] => Array 
    (
     [SiteID] => 146 
     [Amount] => 500.00 
     [TransactionType] => D 
    ) 
[6] => Array 
    (
     [SiteID] => 145 
     [Amount] => 500.00 
     [TransactionType] => W 
    ) 

[7] => Array 
    (
     [SiteID] => 145 
     [Amount] => 500.00 
     [TransactionType] => W 
    ) 

[8] => Array 
    (
     [SiteID] => 146 
     [Amount] => 330.00 
     [TransactionType] => W 
    ) 

[9] => Array 
    (
     [SiteID] => 146 
     [Amount] => 500.00 
     [TransactionType] => W 
    ) 

[10] => Array 
    (
     [SiteID] => 3 
     [Amount] => 500.00 
     [TransactionType] => D 
    ) 

我需要讓所有的交易一樣SITEID的總和,它是TRANSACTIONTYPE例如像:

d - 礦牀,W - 重新demption,R - 刷新

結果:array ([147]=>array([Deposit] => 1000, [Reload]=>3000, [Redemption]=>2000))

注意:147是一個SITEID。

我需要計算存款,重新加載,兌換同一SiteID的所有交易。

如果SiteID存在多次,只需在每個週期添加存款,重新加載和贖回。 :)

請回應。謝謝,並以適當的方式引導我!

+0

所以,你想從大陣裏面總結了所有的值? – 2012-08-09 08:54:23

+0

什麼是輸入? – lvil 2012-08-09 08:54:50

+3

['$ sum = array_sum($ result ['SiteId']);'](http://uk.php.net/manual/en/function.array-sum.php)? – DaveRandom 2012-08-09 08:54:50

回答

0

試試這個

<?php 
$array=array ("SiteID"=>array("Deposit" => 1000, "Reload"=>3000, "Redemption"=>2000)); 
$val=0; 
foreach($array["SiteID"] as $key=>$value){ 
$val=$val+$value; 
} 
echo $val; 
?> 
0
$values = array(
    'Deposit' => 0, 
    'Reload'  => 0, 
    'Redemption' => 0 
); 
foreach ($array as $item) { 
    $values[$item['SiteID']] += array_sum($item['SiteID']); 
} 
print_r($values);