-3

可能重复:
阵列打印

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。请修改代码:(

4

1 回答 1

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']);
于 2012-08-10T08:51:38.010 回答