title: Merging arrays recursively PHP
body: <p>I am using this function two merge recursively arrays:</p>
  
  <pre><code>function array_merge_recursive_distinct(array &amp;$array1, array &amp;$array2) {
  
    $merged = $array1;
  
    foreach($array2 as $key =&gt; &amp;$value) {
  
        if(is_array($value) &amp;&amp; isset($merged[$key]) &amp;&amp; is_array($merged[$key])) {
  
            $merged[$key] = array_merge_recursive_distinct($merged[$key], $value);
  
        } else {
  
            $merged[$key] = $value;
        }
    }
  
    return $merged;
  }
  </code></pre>
  
  <p>For using this function I am doing the following steps:</p>
  
  <ul>
  <li>Declare an empty array $outArray = array();</li>
  <li>Doing a while loop where I collect the information I need
  
  <ul>
  <li>During the while loop I call the array_merge_recursive_distinct function                to fill the empty array recursively</li>
  </ul></li>
  </ul>
  
  <p>However the final array contains only the last information it was gathered during the last while loop. I have tried to find a solution but I haven't succeed until now. What Am I doing wrong? 
  The recursive function takes all the info during the while loops (I have printed the input arrays in the recursive function) but it seems like it overwrites the merged array over and over again.  </p>
  
  <p>Thanks</p>