用PHP正确打印数组

时间:2012-08-16 07:55:21

标签: php arrays printing

如果数组与SiteID不匹配,如何打印数组,只需打印[Deposit] => 0,[重新加载] => 0和[兑换] => 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
    )
)  

但输错了。网站ID的存款,重新加载和赎回=&gt; 4应该等于零。我怎样才能做到这一点?请以正确的方式指导我。提前谢谢。

1 个答案:

答案 0 :(得分:0)

我相信你的方法是错误的,请尝试:

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是什么。