title: Array merge on multidimensional array
body: <p>Either I'm blind or I can't find this problem anywhere here on SO.
  Yesterday I had a problem with merging arrays, which I could fix with the help of SO. Today I have, again, a problem with merging arrays, but this time it's with multidimensional Arrays.</p>
  
  <p>I have an array <code>$usergroup['groups']</code> and an array <code>$usergroup['lang']</code></p>
  
  <p><code>$usergroup['groups']</code> looks like this:</p>
  
  <pre><code>Array
  (
      [0] =&gt; Usergroup_Model Object
          (
              [id] =&gt; 1
              [deleted] =&gt; 0
          )
  
      [1] =&gt; Usergroup_Model Object
          (
              [id] =&gt; 2
              [deleted] =&gt; 0
          )
  
      [2] =&gt; Usergroup_Model Object
          (
              [id] =&gt; 3
              [deleted] =&gt; 0
          )
  
  )
  </code></pre>
  
  <p>And <code>$usergroup['lang']</code> looks like this:</p>
  
  <pre><code>Array
  (
      [0] =&gt; Usergroup_Model Object
          (
              [id] =&gt; 
              [id_usergroup] =&gt; 1
              [name] =&gt; Administratoren
              [id_lang] =&gt; 1
          )
  
      [1] =&gt; Usergroup_Model Object
          (
              [id] =&gt; 
              [id_usergroup] =&gt; 2
              [name] =&gt; Benutzer
              [id_lang] =&gt; 1
          )
  
      [2] =&gt; Usergroup_Model Object
          (
              [id] =&gt; 
              [id_usergroup] =&gt; 3
              [name] =&gt; Gäste
              [id_lang] =&gt; 1
          )
  
  )
  </code></pre>
  
  <p>I want my merged array to look like this:</p>
  
  <pre><code>Array
  (
      [0] =&gt; Usergroup_Model Object
          (
              [id] =&gt; 1
              [id_usergroup] =&gt; 1
              [name] =&gt; Administratoren
              [id_lang] =&gt; 1
              [deleted] =&gt; 0
          )
  
      [1] =&gt; Usergroup_Model Object
          (
              [id] =&gt; 2
              [id_usergroup] =&gt; 2
              [name] =&gt; Benutzer
              [id_lang] =&gt; 1
              [deleted] =&gt; 0
          )
  
      [2] =&gt; Usergroup_Model Object
          (
              [id] =&gt; 3
              [id_usergroup] =&gt; 3
              [name] =&gt; Gäste
              [id_lang] =&gt; 1
              [deleted] =&gt; 0
          )
  
  )
  </code></pre>
  
  <p><a href="http://www.whathaveyoutried.com" rel="nofollow">What have I tried?</a></p>
  
  <p>I've tried several merging functions (<code>array_merge()</code> and <code>array_merge_recursive()</code>) of PHP, the closest result I got was, that the second Array (<code>['lang']</code>) overwrote the first Array (<code>['groups']</code>). To fix that, I tried to remove the empty values on the <code>lang</code> Array (which is always <code>id</code>). But that does not fix it. The code - at the moment - looks like this:</p>
  
  <pre><code>public static function getAll()
  {
    $usergroup['groups'] = self::find();
    $usergroup['lang'] = self::findInTable(array(
      'id_lang' =&gt; Language_Model::getDefaultLanguage()
    ), self::dbTranslationTable);
    foreach ($usergroup as $ug) {
      $ug = array_filter($ug, function($val) {
        return $val != '';
      });
    }
    return array_merge($ug);
  }
  </code></pre>
  
  <p>The array_merge() on the return command doesn't seem to do anything at all, so I'm probably not gathering the data correctly or I mess something up with the Arrays (forgetting to add [], or I don't know...). I kinda miss the forest for the trees here.</p>
  
  <p>Any suggestions in which direction I could go?</p>
  
  <p><strong>Edit:</strong> With the code provided by Pé de Leão I was able to solve the problem. My function now looks like this:</p>
  
  <pre><code>public static function getAll()
  {
    $usergroup['groups'] = self::find();
    $usergroup['lang'] = self::findInTable(array(
      'id_lang' =&gt; Language_Model::getDefaultLanguage()
    ), self::dbTranslationTable);
    $out = array();
    foreach ($usergroup['groups'] as $key =&gt; $value) {
      $out[] = (object) array_merge((array) $usergroup['lang'][$key], (array) $value);
    }
    return $out;
  }
  </code></pre>
  
  <p>And the result is exactly how I wanted it!</p>