2012-08-10 35 views
-3

可能重複:
Printing of Array陣列的印刷

public function computeGHComponents() 
{ 
    error_reporting (E_ALL^ E_NOTICE);   

    $total = null; 

    $result=array(); 


    foreach ($this->transaction as $t){ 
     $amount = (float) $t['Amount']; 

     if (isset($this->totals[ $t['SiteID'] ][ $t['TransactionType'] ])){ 
      $this->totals[ $t['SiteID'] ][ $t['TransactionType'] ] += (float) $amount; 
     } else { 
      $this->totals[ $t['SiteID'] ][ $t['TransactionType'] ] = (float) $amount; 
     } 
    } 

    foreach($this->totals as $key => $value) 
    { 
     $result =   array_merge($result,array($key=>array("Deposit"=>$value['D'],"Redemption"=>$value['W'],"Reload"=>$value['R']))); 
    } 

    print_r($result); 

} 

的關鍵應該是SITEID,我該怎麼辦呢?

我需要這種輸出:

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

的關鍵應該是SITEID。請修改了代碼:(

+0

不,我只是打印一次:( – Happy 2012-08-10 08:39:12

+0

有人可以幫我嗎? – Happy 2012-08-10 08:45:48

+0

與我一樣,很多StackOverflow的用戶都住在北美,根據時區不同,它的時間是2-5am。在10分鐘內預計答案有點冒失。 – 2012-08-10 08:52:23

回答

1

這是正常的,因爲你正在使用array_merge(),看看在文檔:http://php.net/manual/en/function.array-merge.php

值用數字鍵輸入數組中將會 遞增鍵重新編號從零結果數組中

所以你SITEID這是關鍵將重新編號。

開始,然後讓你的鑰匙,它會更好要做到這一點:

$result[$key] = array("Deposit"=>$value['D'], "Redemption"=>$value['W'], "Reload"=>$value['R']); 
+0

yehey!我終於完成了! :) WarfarA,這是一個很大的幫助。它的作品!非常感謝你和更多的權力:) – Happy 2012-08-10 08:57:39