0

我这里有数据:

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

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

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

我已经完成了重新加载、赎回和存款的计算并且它正在工作,但是如果站点 ID 没有存款 [D] 或重新加载 [R] 或赎回 [W] 的交易,它应该等于 0。我的代码在这儿:

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

      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)


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

       }
     print_r($this->result); 

    } 

结果应如下所示:

 Array
 (
[147] => Array
    (
        [Deposit] => 1000
        [Redemption] => 0
        [Reload] => 0
    )

[145] => Array
    (
        [Deposit] => 0
        [Redemption] => 500.00
        [Reload] => 0
    )

 )

在此先感谢,请以正确的方式指导我。

4

2 回答 2

0

描述含糊不清,问题中缺少信息。所以这是一种盲目的尝试,它可能是你所要求的,但也可能不是:

在最后一个 foreach 内的赋值中,您可以添加 0 作为默认值:

$this->result[$key] = array(
  "Deposit"    => array_key_exists('D',$value) ? $value['D'] : 0,            
  "Redemption" => array_key_exists('W',$value) ? $value['W'] : 0,           
  "Reload"     => array_key_exists('R',$value) ? $value['R'] : 0             
);
于 2012-08-15T03:03:29.007 回答
0

我会专注于只做一个循环。

foreach ($this->transaction as $key => $t){

   $amount = $t['Amount'];

   // set up the array with each postion equal to zero
   $tranTypes = array("Deposit"= 0, "Redemption"=> 0 , "Reload"= 0);

   $transaction = $t['TransactionType'];

   switch($transaction){
     case "D":
        $tranTypes['Deposit'] = $amount;
     break;

     case "R":
         $tranTypes['Redemption'] = $amount;
     break;

     case "W":
         $tranTypes['Reload'] = $amount;
     break;

     }

    $this->result[$key] = $transaction;
}

或这个

 foreach($this->totals as $key => $value)


    {

     $this->result[$key]['Deposit']    = isset($value['D']) ? $value['D'] : 0;
     $this->result[$key]['Redemption'] = isset($value['W']) ? $value['W'] : 0; 
     $this->result[$key]['Reload']     = isset($value['R']) ? $value['R'] : 0;
    }
于 2012-08-15T03:15:11.250 回答