Search code examples
phparraysbindingmergearray-merge

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


I have two functions here:

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

the output of bindOwnerToSites() shown like this:

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

  )

Here's the code for the second function:

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

    } 

And the output of computeGHComponents() shown like this:

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

  )

Now, I need to bind the result of the two functions, I'll try on my own but there something wrong with my codes, I need your help, I put everything here so I hope you better know what I suppose to achieved. Here's my code in binding :

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

  }

The result should be like this, How can I have this kind of result? I need your patience with this question, I'm just a beginner programmer, so hope you understand.

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

 )

Thanks in advance and Please guide me in proper way.


Solution

  • There is the plus operator for merging arrays (when keys should be preserved):

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

    In your case this can be utilized like this:

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