title: How to merge multidimensional arrays while preserving keys?
body: <p>Is there a way for these arrays</p>
  
  <pre><code>$array1 = array(
      '21-24' =&gt; array(
          '1' =&gt; array("...")
      )
  );
  
  $array2 = array(
      '21-24' =&gt; array(
          '7' =&gt; array("..."),
      )
  );
  
  $array3 = array(
      '25 and over' =&gt; array(
          '1' =&gt; array("...")
      )
  );
  
  $array4 = array(
      '25 and over' =&gt; array(
          '7' =&gt; array("...")
      )
  );
  </code></pre>
  
  <p>to be merged which result into the array below?</p>
  
  <pre><code>array(
      '21-24' =&gt; array(
          '1' =&gt; array("..."),
          '7' =&gt; array("...")
      ),      
      '25 and over' =&gt; array(
          '1' =&gt; array("..."),
          '7' =&gt; array("...")
      )
  );
  </code></pre>
  
  <p><strong>NOTE</strong>:</p>
  
  <ul>
  <li>I don't have control over the array structure so any solution that requires changing the structure is not what I am looking for</li>
  <li>I am mainly interested in preserving the keys of the first 2 levels but a more robust solution is one that can handle all level.</li>
  </ul>
  
  <p>I tried using <a href="http://php.net/manual/en/function.array-merge-recursive.php">array_merge_recursive()</a> like this</p>
  
  <pre><code>$x = array_merge_recursive($array1, $array2);
  $x = array_merge_recursive($x, $array3);
  $x = array_merge_recursive($x, $array4);
  </code></pre>
  
  <p>but it resulted in</p>
  
  <pre><code> array(
      '21-24' =&gt; array(
          '1' =&gt; array("..."),
          '2' =&gt; array("...")
      ),      
      '25 and over' =&gt; array(
          '1' =&gt; array("..."),
          '2' =&gt; array("...")
      )
  );
  </code></pre>