<p>I have two arrays of animals (for example).</p>

<pre><code>$array = array(
    array(
        'id' =&gt; 1,
        'name' =&gt; 'Cat',
    ),
    array(
        'id' =&gt; 2,
        'name' =&gt; 'Mouse',
    )
);

$array2 = array(
    array(
        'id' =&gt; 2,
        'age' =&gt; 321,
    ),
    array(
        'id' =&gt; 1,
        'age' =&gt; 123,
    )
);
</code></pre>

<p>How can I merge the two arrays into one by the ID?</p>

First off, why don't you use the ID as the index (or key, in the mapping-style array that php arrays are imo)?
```
```
$array = array(
    1 => array(
        'name' => 'Cat',
    ),
    2 => array(
        'name' => 'Mouse',
    )
);
```
```
after that you'll have to foreach through one array, performing array_merge on the items of the other:
```
```
foreach($array2 as $key=>$value) {
  if(!is_array($array[$key])) $array[$key] = $value;
  else $array[$key] = array_merge($array[key], $value); 
}
```
```
Something like that at least. Perhaps there's a better solution?