可能重复:
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 :(得分:1)
这是正常的,因为您正在使用array_merge(),请查看文档:{{3}}
带有数字键的输入数组中的值将重新编号 在结果数组中从零开始递增键。
因此,作为密钥的SiteID将重新编号。
然后,为了保留你的钥匙,最好这样做:
$result[$key] = array("Deposit"=>$value['D'], "Redemption"=>$value['W'], "Reload"=>$value['R']);