Search code examples
phparraysprintingarray-merge

How to bind the two array results and Print it in a right output PHP


I have two data here:

The result of this first function:

Array
(
[2] => Array
    (
        [SiteID] => 2
        [Balance] => 19000.00
        [MinBalance] => 100000.00
        [MaxBalance] => 1000000.00
        [OwnerAID] => 83
        [GroupID] => 1
    )

 [3] => Array
    (
        [SiteID] => 3
        [Balance] => 94000.99
        [MinBalance] => 100000.00
        [MaxBalance] => 500000.00
        [OwnerAID] => 17
        [GroupID] => 1
    )

[4] => Array
    (
        [SiteID] => 4
        [Balance] => 1000000.00
        [MinBalance] => 100000.00
        [MaxBalance] => 1000000.00
        [OwnerAID] => 12
        [GroupID] => 1
    )

)

The result of the second function:

 Array
(
 [3] => Array
    (
        [Deposit] => 459000
        [Reload] => 169100
        [Redemption] => 703576
    )

[2] => Array
    (
        [Deposit] => 1500
        [Reload] => 1000
        [Redemption] => 1000
    )
 )

Now, I need to bind the result of thie two functions, I try it with this codes:

    public function bindGHComponentsToSites()
    {
   error_reporting (E_ALL^ E_NOTICE);  
   $combine = array();

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

But, I got the wrong output, the condition was, If the SiteID of bindownertosites doesn't match to the SiteId of Computeghcomponents then just print the value of the Deposit, Reload and Redemption should be equal to zero.

The Deposit, Reload and Redemption are all equal to zero which is wrong.Here's should be the right result:

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

Is it possible to have this kind of result? Please help me guys, I've been working with this almost one week, but I always got a wrong output. I appreciate to your response.I put everything here to make clarify those things.Kindly review my question before you answer. Please help me and guide me in proper way. Thank you in advance.


Solution

  • The this will do what you expected, so simple

    Try

    foreach (array_keys($this->arrays) as $value) {
        $default = array('Deposit' => 0, 'Reload' => 0, 'Redemption' => 0);
        if (array_key_exists($value, $this->result)) {
            $temp = array_merge($this->arrays[$value], $this->result[$value]);
            $this->arrays[$value] = $temp;
        } else {
            $temp = array_merge($this->arrays[$value], $default);
            $this->arrays[$value] = $temp;
        }
    }
    print_r($this->arrays);