Counting an eliminating duplicate associative values in an array
<p>I have the following issue, I have an array with the name $data</p>

<p>Within this array I have something like</p>

<pre><code>[6] =&gt; Array
        (
            [code] =&gt; 642
            [total] =&gt; 1708
        )

    [7] =&gt; Array
        (
            [code] =&gt; 642
            [total] =&gt; 53
        )

    [8] =&gt; Array
        (
            [code] =&gt; 642
            [total] =&gt; 1421
        )
</code></pre>

<p>In some elements the code value is the same, now what I want to do is merge all the elements with the same code value together, and adding the totals together. I tried doing this in a foreach loop, but does not seem to work.</p>

<p>I do something like this</p>

<pre><code>$old_lc = null;
        $old_lcv = 0;
        $count = 0;
        $dd = null;


        foreach($data as $d){

            if($d['code'] == $old_lc){
                $d['total'] = $d['total'] + $old_lcv;
                $count--;
                $dd[$count]['code'] = $d['code'];
                $dd[$count]['total'] = $d['total'];

            }else{
                $dd[$count]['code'] = $d['code'];
                $dd[$count]['total'] = $d['total'];
                $count++;

            }

            $old_lc = $d['code'];
            $old_lcv = $d['total'];
        }

        $data = $dd;
</code></pre>

<p>But this does not seem to work. Also I need the $data array to keep the keys, and should remain in the same format</p>
