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


How to bind the result of two functions using array in PHP

>我这里有两个函数:

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

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

有用于合并数组的加号运算符(当应该保留键时):

<?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];
}