0

如果数组与 SiteID 不匹配,如何打印数组,只需打印 [Deposit] => 0、[Reload] => 0 和 [Redemption] => 0。

这是我的第一个功能:

   public function bindOwnerToSites(){

     error_reporting (E_ALL^ E_NOTICE);  

    foreach( $this->balance as $keysaa =>$key_valuesa)//getsitebalance
            { 
                foreach( $this->sites as $keys=>$key_values)//getsites
                  {

                        if  ($key_values['SiteID'] == $key_valuesa['SiteID'])
                        {

                         $this->arrays[$key_valuesa['SiteID']] = array('SiteID'=>$key_valuesa['SiteID'],'Balance'=>$key_valuesa['Balance'],'MinBalance'=>$key_valuesa['MinBalance'],'MaxBalance'=>$key_valuesa['MaxBalance'],'OwnerAID'=>$key_values['OwnerAID'],'GroupID'=>1);    

                        }
                 }

            }
        print_r ($this->arrays,$return=null);  
    }

这是我的第二个功能:

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

      $totals = NULL;

      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 $keyb => $value)

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

       }
      echo "<pre>";

     print_r($this->result);

    } 

现在我需要绑定两个函数的结果,我试试这段代码:

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


     foreach ($this->arrays as $keys => $data) {


       foreach($this->result as $keyss => $value){

            if($data['SiteID'] == $keyss){


              $merged = array_merge((array)$data, (array)$value);

             }
             else if ($data['SiteID'] != $keyss){

                  $val = array('Deposit'=>0, 'Reload'=>0, 'Redemption'=>0);

                  $merged = array_merge((array)$data, (array)$val);
             }

        }

           $this->combined[$data['SiteID']] = $merged;

     } 

 print_r($this->combined);
}  

但函数的结果如下所示:

Array
(
[2] => Array
    (
        [SiteID] => 2
        [Balance] => 19000.00
        [MinBalance] => 100000.00
        [MaxBalance] => 1000000.00
        [OwnerAID] => 83
        [GroupID] => 1
        [Deposit] => 1500
        [Reload] => 1000
        [Redemption] => 1000
    )

[3] => Array
    (
        [SiteID] => 3
        [Balance] => 94000.99
        [MinBalance] => 100000.00
        [MaxBalance] => 500000.00
        [OwnerAID] => 17
        [GroupID] => 1
        [Deposit] => 459000
        [Reload] => 169100
        [Redemption] => 703576
    )

[4] => Array
    (
        [SiteID] => 3
        [Balance] => 94000.99
        [MinBalance] => 100000.00
        [MaxBalance] => 500000.00
        [OwnerAID] => 17
        [GroupID] => 1
        [Deposit] => 459000
        [Reload] => 169100
        [Redemption] => 703576
    )
)  

这里应该是正确的输出:

 Array
(
[2] => Array
    (
        [SiteID] => 2
        [Balance] => 19000.00
        [MinBalance] => 100000.00
        [MaxBalance] => 1000000.00
        [OwnerAID] => 83
        [GroupID] => 1
        [Deposit] => 1500
        [Reload] => 1000
        [Redemption] => 1000
    )

[3] => Array
    (
        [SiteID] => 3
        [Balance] => 94000.99
        [MinBalance] => 100000.00
        [MaxBalance] => 500000.00
        [OwnerAID] => 17
        [GroupID] => 1
        [Deposit] => 459000
        [Reload] => 169100
        [Redemption] => 703576
    )

[4] => Array
    (
        [SiteID] => 3
        [Balance] => 94000.99
        [MinBalance] => 100000.00
        [MaxBalance] => 500000.00
        [OwnerAID] => 17
        [GroupID] => 1
        [Deposit] => 0
        [Reload] => 0
        [Redemption] => 0
    )
)  

但我得到了错误的输出。Site ID => 4的存款、充值和赎回应该等于0。我怎样才能做到这一点?请以正确的方式指导我。先感谢您。

4

1 回答 1

0

我相信您的 foreach 方法错误,请尝试:

foreach($this->result as $keyss => $value){
    if($keyss == 'SiteID'){
        if($data['SiteID'] == $value){
            $merged = array_merge((array)$data, (array)$value);
        }
        else if ($data['SiteID'] != $value){
            $val = array('Deposit'=>0, 'Reload'=>0, 'Redemption'=>0);
            print_r($val);
            $merged = array_merge((array)$data, (array)$val);
        }
    }
}

虽然我不能确定,因为我不知道是什么$this->result

根据我所看到的,这也可以工作:

foreach($this->result as $keyss => $value){
        if($data['SiteID'] == $value['SiteID']){
            $merged = array_merge((array)$data, (array)$value);
        }
        else if ($data['SiteID'] != $value['SiteID']){
            $val = array('Deposit'=>0, 'Reload'=>0, 'Redemption'=>0);
            print_r($val);
            $merged = array_merge((array)$data, (array)$val);
        }
}

但是我们也不知道是什么$this->result

于 2012-08-16T08:07:04.270 回答