如何在PHP中使用数组绑定两个函数的结果

时间:2012-08-11 13:22:44

标签: php arrays binding merge array-merge

我在这里有两个功能:

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);  
    }

bindOwnerToSites()的输出如下所示:

Array
(
[1] => Array
    (
        [SiteID] => 1
        [Balance] => 2000
        [MinBalance] => 1000
        [MaxBalance] => 500
        [OwnerAID] => 1
        [GroupID] => 1
    )

  )

这是第二个函数的代码:

    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); 

    } 

computeGHComponents()的输出显示如下:

Array
(
[1] => Array
    (
        [Deposit] => 10000
        [Redemption] => 500.00
        [Reload] => 200.00
    )

  )

现在,我需要绑定两个函数的结果,我会自己尝试但是我的代码有问题,我需要你的帮助,我把所有东西都放在这里所以我希望你更好地知道我的想法实现。这是我的绑定代码:

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

    $combined = array();

    foreach($this->arrays as $key => $key_value){

                 foreach($this->result as $keys => $key_values){   

               $combined[$key_values['SiteID']] = array_merge((array)$key_value, (array)$key_values);

          }


     }  
       print_r($combined);

  }

结果应该是这样的,我怎么能有这样的结果?我需要你对这个问题的耐心,我只是一个初学程序员,所以希望你理解。

Array
(
[1] => Array
    (
        [SiteID] => 1
        [Balance] => 2000
        [MinBalance] => 1000
        [MaxBalance] => 500
        [OwnerAID] => 1
        [GroupID] => 1
        [Deposit] => 10000.00
        [Redemption] =>  500.00
        [Reload] => 200.00
    )

 )

提前致谢,请以适当的方式指导我。

1 个答案:

答案 0 :(得分:2)

用于合并数组的 plus运算符(当应保留键时):

<?php
$array1 = array('foo' => 'bar');
$array2 = array('baz' => 'trololol');

print_r($array1 + $array2); // yields "Array ( [foo] => bar [baz] => trololol )"

在你的情况下,这可以像这样使用:

$combined = array();
foreach ($this->arrays as $siteId => $data) {
    $combined[$siteId] = $data + $this->result[$siteId];
}