title: PHP Identifying & manipulating duplicates in multi-dimensional array
body: <p>I have a multi-dimensional array of values posted from a form and I am trying to not just de-dupe them, but sum the values from the duplicated arrays so that these can be posted as one, larger value.</p>
  
  <p>The array posted is as follows:</p>
  
  <pre><code>Array
  (
  [0] =&gt; Array
      (
          [0] =&gt; 4
          [1] =&gt; 27/11/2013
      )
  
  [1] =&gt; Array
      (
          [0] =&gt; 4
          [1] =&gt; 27/11/2013
      )
  
  [2] =&gt; Array
      (
          [0] =&gt; 2
          [1] =&gt; 
      )
  
  [3] =&gt; Array
      (
          [0] =&gt; 2
          [1] =&gt; 
      )
  
  )
  </code></pre>
  
  <p>For each sub-array, value 0 is a quantity and value 1 is a date (which is optional). The final entry in the array is a qty of 2 with a blank date which was added automatically because the required total was 12.</p>
  
  <p>I am then looking through the sub-arrays using a foreach loop (below) to check for duplicate dates.</p>
  
  <pre><code>foreach ($qty_date_array as $key =&gt; $value)
  {
      if(in_array($value[1], $exists) &amp;&amp; !in_array($value[1], $duplicates))
      {
          $duplicates[] = $value[1]; 
      }
      else 
      {
          $exists[] = $value[1]; 
      }
  </code></pre>
  
  <p>The output of $duplicates is...</p>
  
  <pre><code>Array
  (
      [0] =&gt; 27/11/2013
      [1] =&gt; 
  )
  </code></pre>
  
  <p>I guess I then want to look at the values reached in the array and foreach of them, find the associated original qty value(s) and sum them, but I'm not sure how to proceed further. </p>
  
  <p>Array_search will only return the key, so I guess that is out (and I cannot write the Qty as the key because it may be a duplicate value). Other array functions i've dabbled with, such as array_unique, collapse the array based on the indexes, so lose data.</p>
  
  <p>The final multidimensional array would ideally look something like this:</p>
  
  <pre><code>Array
  (
  [0] =&gt; Array
      (
          [0] =&gt; 8
          [1] =&gt; 27/11/2013
      )
  
  [2] =&gt; Array
      (
          [0] =&gt; 4
          [1] =&gt; 
      )
  )
  </code></pre>
  
  <p>If someone can suggest a more efficient way to achieve this, I'm open to all suggestions!</p>