title: How to merge two arrays and sum the values of duplicate keys?
body: <p>I would like to merge array by conditions. If array keys match then add the values, if not then retain the value.</p>
  
  <p>Here are my arrays:</p>
  
  <pre><code>Array1
  (   
      [1] =&gt; 199
      [3] =&gt; 1306
      [5] =&gt; 199
  )
  
  Array2
  (  
     [3] =&gt; 199
     [4] =&gt; 199
  )
  </code></pre>
  
  <p>My desired result is:</p>
  
  <pre><code>Result
  (  
     [1] =&gt; 199
     [3] =&gt; 1505
     [4] =&gt; 199
     [5] =&gt; 199    
  )
  </code></pre>
  
  <p>I used if-else conditions, but it's repeating the value which is already matched.</p>
  
  <p>Here is my coding attempt:</p>
  
  <pre><code>$all=array();   
  foreach($sall as $sskey =&gt; $ssvalue){
      foreach($upgradesall as $uukey =&gt; $uuvalue){
          //$sskey==$uukey?$all[] = array("id"=&gt;$sskey, "amount"=&gt;$ssvalue+$uuvalue):($sskey!=$uukey? $all[] = array("id"=&gt;$sskey, "amount"=&gt;$ssvalue):($uukey!=$sskey?$all[] = array("id"=&gt;$uukey, "amount"=&gt;$uuvalue):''));
          if($sskey===$uukey){
              $all[] = array("id"=&gt;$sskey, "amount"=&gt;$ssvalue+$uuvalue);
          }elseif($sskey!=$uukey){
              $all[] = array("id"=&gt;$sskey, "amount"=&gt;$ssvalue);
          }elseif($uukey!=$sskey){
              $all[] = array("id"=&gt;$uukey, "amount"=&gt;$uuvalue);
          }
      }
  }   
  </code></pre>