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

[{'answer_id': 25155, 'author': 'Erik van Brakel', 'author_id': 909, 'author_profile': 'https://Stackoverflow.com/users/909', 'pm_score': 2, 'selected': False, 'text': "<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)?</p>\n\n<pre><code>$array = array(\n    1 =&gt; array(\n        'name' =&gt; 'Cat',\n    ),\n    2 =&gt; array(\n        'name' =&gt; 'Mouse',\n    )\n);\n</code></pre>\n\n<p>after that you'll have to foreach through one array, performing array_merge on the items of the other:</p>\n\n<pre><code>foreach($array2 as $key=&gt;$value) {\n  if(!is_array($array[$key])) $array[$key] = $value;\n  else $array[$key] = array_merge($array[key], $value); \n}\n</code></pre>\n\n<p>Something like that at least. Perhaps there's a better solution?</p>\n"}, {'answer_id': 25166, 'author': 'Ross', 'author_id': 2025, 'author_profile': 'https://Stackoverflow.com/users/2025', 'pm_score': 3, 'selected': True, 'text': "<p>This does what Erik suggested (id no. as array key) and merges vlaues in <code>$array2</code> to <code>$results</code>.</p>\n\n<pre><code>$results = array();\n\nforeach($array as $subarray)\n{\n    $results[$subarray['id']] = array('name' =&gt; $subarray['name']);\n}\n\nforeach($array2 as $subarray)\n{\n    if(array_key_exists($subarray['id'], $results))\n    {\n        // Loop through $subarray would go here if you have extra \n        $results[$subarray['id']]['age'] = $subarray['age'];\n    }\n}\n</code></pre>\n"}, {'answer_id': 25215, 'author': 'Andy', 'author_id': 1993, 'author_profile': 'https://Stackoverflow.com/users/1993', 'pm_score': -1, 'selected': False, 'text': "<pre><code>$new = array();\nforeach ($array as $arr) {\n    $match = false;\n    foreach ($array2 as $arr2) {\n        if ($arr['id'] == $arr2['id']) {\n           $match = true;\n           $new[] = array_merge($arr, $arr2);\n           break;\n        }\n    }\n    if ( !$match ) $new[] = $arr;\n}\n</code></pre>\n"}, {'answer_id': 25221, 'author': 'Ross', 'author_id': 2025, 'author_profile': 'https://Stackoverflow.com/users/2025', 'pm_score': 3, 'selected': False, 'text': '<p>@Andy</p>\n\n<blockquote>\n  <p><a href="http://se.php.net/array_merge" rel="noreferrer">http://se.php.net/array_merge</a></p>\n</blockquote>\n\n<p>That was my first thought but it doesn\'t quite work - however <a href="http://www.php.net/manual/en/function.array-merge-recursive.php" rel="noreferrer">array_merge_recursive</a> might work - too lazy to check right now.</p>\n'}, {'answer_id': 25237, 'author': 'AnnanFay', 'author_id': 2118, 'author_profile': 'https://Stackoverflow.com/users/2118', 'pm_score': 1, 'selected': False, 'text': '<p><a href="https://stackoverflow.com/questions/25147?sort=oldest#25215">@Andy</a></p>\n\n<p>I\'ve already looked at that and didn\'t see how it can help merge multidimensional arrays. Maybe you could give an example.</p>\n\n<p><a href="https://stackoverflow.com/questions/25147?sort=oldest#25214">@kevin</a></p>\n\n<p>That is probably what I will need to do as I think the code below will be very slow.\nThe actual code is a bit different because I\'m using ADOdb (and ODBC for the other query) but I\'ll make it work and post my own answer.</p>\n\n<p>This works, however I think it will be very slow as it goes through the second loop every time:</p>\n\n<pre><code>foreach($array as &amp;$animal)\n{\n    foreach($array2 as $animal2)\n    {\n        if($animal[\'id\'] === $animal2[\'id\'])\n        {\n            $animal = array_merge($animal, $animal2);\n            break;\n        }\n    }\n}\n</code></pre>\n'}, {'answer_id': 25347, 'author': 'UnkwnTech', 'author_id': 115, 'author_profile': 'https://Stackoverflow.com/users/115', 'pm_score': 2, 'selected': False, 'text': '<pre><code>&lt;?php\n      $a = array(\'a\' =&gt; \'1\', \'b\' =&gt; array(\'t\' =&gt; \'4\', \'g\' =&gt; array(\'e\' =&gt; \'8\')));\n      $b = array(\'c\' =&gt; \'3\', \'b\' =&gt; array(\'0\' =&gt; \'4\', \'g\' =&gt; array(\'h\' =&gt; \'5\', \'v\' =&gt; \'9\')));\n      $c = array_merge_recursive($a, $b);\n      print_r($c);\n?&gt;\n</code></pre>\n\n<p><a href="http://php.net/manual/en/function.array-merge-recursive.php" rel="nofollow noreferrer">array_merge_recursive — Merge two or more arrays recursively</a></p>\n\n<p>outputs:</p>\n\n<pre><code>        Array\n(\n    [a] =&gt; 1\n    [b] =&gt; Array\n        (\n            [t] =&gt; 4\n            [g] =&gt; Array\n                (\n                    [e] =&gt; 8\n                    [h] =&gt; 5\n                    [v] =&gt; 9\n                )\n\n            [0] =&gt; 4\n        )\n\n    [c] =&gt; 3\n)\n</code></pre>\n'}, {'answer_id': 14685647, 'author': 'Uzair Bin Nisar', 'author_id': 1276847, 'author_profile': 'https://Stackoverflow.com/users/1276847', 'pm_score': 1, 'selected': False, 'text': '<p>I would rather prefer <code>array_splice</code> over <code>array_merge</code> because of its performance issues, my solution would be:</p>\n\n<pre><code>&lt;?php \narray_splice($array1,count($array1),0,$array2);\n?&gt;\n</code></pre>\n'}, {'answer_id': 14685886, 'author': 'Marcin Majchrzak', 'author_id': 546081, 'author_profile': 'https://Stackoverflow.com/users/546081', 'pm_score': 1, 'selected': False, 'text': "<pre><code>foreach ($array as $a)\n    $new_array[$a['id']]['name'] = $a['name'];\n\nforeach ($array2 as $a)\n    $new_array[$a['id']]['age'] = $a['age'];\n</code></pre>\n\n<p>and this is result:</p>\n\n<pre><code>    [1] =&gt; Array\n        (\n            [name] =&gt; Cat\n            [age] =&gt; 123\n        )\n\n    [2] =&gt; Array\n        (\n            [name] =&gt; Mouse\n            [age] =&gt; 321\n        )\n</code></pre>\n"}, {'answer_id': 16175388, 'author': 'Hamed J.I', 'author_id': 2294072, 'author_profile': 'https://Stackoverflow.com/users/2294072', 'pm_score': 1, 'selected': False, 'text': '<pre><code>&lt;?php\n$array1 = array("color" =&gt; "red", 2, 4);\n$array2 = array("a", "b", "color" =&gt; "green", "shape" =&gt; "trapezoid", 4);\n$result = array_merge($array1, $array2);\nprint_r($result);\n?&gt;\n</code></pre>\n'}, {'answer_id': 17268429, 'author': 'jb510', 'author_id': 554469, 'author_profile': 'https://Stackoverflow.com/users/554469', 'pm_score': 1, 'selected': False, 'text': '<p>With PHP 5.3 you can do this sort of merge with array_replace_recursive()</p>\n\n<p><a href="http://www.php.net/manual/en/function.array-replace-recursive.php" rel="nofollow">http://www.php.net/manual/en/function.array-replace-recursive.php</a></p>\n\n<p>You\'re resultant array should look like:</p>\n\n<pre><code>Array (\n    [0] =&gt; Array\n        (\n            [id] =&gt; 2\n            [name] =&gt; Cat\n            [age] =&gt; 321\n        )\n\n    [1] =&gt; Array\n        (\n            [id] =&gt; 1\n            [name] =&gt; Mouse\n            [age] =&gt; 123\n        )\n)\n</code></pre>\n\n<p>Which is what I think you wanted as a result.</p>\n'}]
['https://Stackoverflow.com/questions/25147', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2118/']