title: How to merge two arrays by summing the merged values
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/1496682/php-how-to-sum-values-of-the-array-of-the-same-key">PHP: How to sum values of the array of the same key</a>  </p>
  </blockquote>
  
  
  
  <p>I am looking for an <code>array_merge()</code> function that does NOT replace values, but ADDS them. </p>
  
  <p>Example, this is the code I am trying: </p>
  
  <pre><code>    echo "&lt;pre&gt;"; 
  
      $a1 = array(
           "a" =&gt; 2
          ,"b" =&gt; 0
          ,"c" =&gt; 5
      );
  
      $a2 = array(
           "a" =&gt; 3
          ,"b" =&gt; 9
          ,"c" =&gt; 7
          ,"d" =&gt; 10
      );
  
      $a3 = array_merge($a1, $a2);
      print_r($a3); 
  </code></pre>
  
  <p>Sadly, this outputs this: </p>
  
  <pre><code>Array
  (
      [a] =&gt; 3
      [b] =&gt; 9
      [c] =&gt; 7
      [d] =&gt; 10
  )
  </code></pre>
  
  <p>I then tried, instead of <code>array_merge</code>, just simply adding the two arrays </p>
  
  <pre><code>$a3 = $a1 + $a2;
  </code></pre>
  
  <p>But this outputs </p>
  
  <pre><code>Array
  (
      [a] =&gt; 2
      [b] =&gt; 0
      [c] =&gt; 5
      [d] =&gt; 10
  )
  </code></pre>
  
  <p>What I truly want is to be able to pass as many arrays as needed, and then get their sum. So in my example, I want the output to be: </p>
  
  <pre><code>Array
  (
      [a] =&gt; 5
      [b] =&gt; 9
      [c] =&gt; 12
      [d] =&gt; 10
  )
  </code></pre>
  
  <p>Of course I can schlepp and build some function with many <code>foreach</code> etc, but am looking or a smarter, cleaner solution. Thanks for any pointers! </p>