Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an array that I need to process by adding the values, then removing any added duplicates.

Here's the Original Array...

Array (     
[0] => Array ( [PID] => 2872 [QTY] => 1 ) 
[1] => Array ( [PID] => 3145 [QTY] => 2 ) 
[2] => Array ( [PID] => 3107 [QTY] => 1 ) 
[3] => Array ( [PID] => 2739 [QTY] => 1 ) 
[4] => Array ( [PID] => 3137 [QTY] => 1 ) 
[5] => Array ( [PID] => 3107 [QTY] => 1 ) 
[6] => Array ( [PID] => 2739 [QTY] => 1 ) 
[7] => Array ( [PID] => 3107 [QTY] => 1 ) 
[8] => Array ( [PID] => 3137 [QTY] => 4 ) 
[9] => Array ( [PID] => 3551 [QTY] => 1 ) 
[10] => Array ( [PID] => 3107 [QTY] => 1 ) 
[11] => Array ( [PID] => 3107 [QTY] => 1 ) 
[12] => Array ( [PID] => 3137 [QTY] => 1 ) 
[13] => Array ( [PID] => 3551 [QTY] => 2 ) 
[14] => Array ( [PID] => 3136 [QTY] => 1 ) 
[15] => Array ( [PID] => 3137 [QTY] => 1 ) 
[16] => Array ( [PID] => 3032 [QTY] => 1 ) 
[17] => Array ( [PID] => 3551 [QTY] => 1 ) 
[18] => Array ( [PID] => 3107 [QTY] => 1 )
[19] => Array ( [PID] => 3459 [QTY] => 1 ) 
[20] => Array ( [PID] => 3141 [QTY] => 1 )
[21] => Array ( [PID] => 2724 [QTY] => 1 ) 
[22] => Array ( [PID] => 2743 [QTY] => 1 )
[23] => Array ( [PID] => 3139 [QTY] => 2 )
[24] => Array ( [PID] => 3137 [QTY] => 2 ) 
[25] => Array ( [PID] => 3107 [QTY] => 1 ) 
)

What I need to do is take this array and Add the [QTY] values from the same [PID] values then after this end up with an array like this...

Array ( 
[0] => Array ( [PID] => 2724 [QTY] => 1 ) 
[1] => Array ( [PID] => 2739 [QTY] => 2 ) 
[2] => Array ( [PID] => 2743 [QTY] => 1 )
[3] => Array ( [PID] => 2872 [QTY] => 1 ) 
[4] => Array ( [PID] => 3032 [QTY] => 1 ) 
[5] => Array ( [PID] => 3107 [QTY] => 7 ) 
[6] => Array ( [PID] => 3136 [QTY] => 1 ) 
[7] => Array ( [PID] => 3137 [QTY] => 9 ) 
[8] => Array ( [PID] => 3139 [QTY] => 2 )
[9] => Array ( [PID] => 3141 [QTY] => 1 )
[10] => Array ( [PID] => 3145 [QTY] => 2 ) 
[11] => Array ( [PID] => 3459 [QTY] => 1 ) 
[12] => Array ( [PID] => 3551 [QTY] => 4 ) 
)

So add all the QTY then remove the duplicates.

I'm not quite sure the best way to proceed here. Any suggestions?

share|improve this question
    
Take a look @ stackoverflow.com/questions/1861682/… –  R.S Nov 9 '12 at 23:41

2 Answers 2

up vote 1 down vote accepted

Id just use a simple loop:

$combined = array();
// $org will be the original structure

foreach($org as $id => $data) {
   $pid = $data['PID'];
   if(!isset($combined[$pid])) {
     $combined[$pid] = $data;
   } else {
     $combined[$pid]['QTY'] += $data['QTY'];
   }
}

// return keys to normal indexs instead of the PID
array_values($combined);
share|improve this answer
    
That's what I was looking for, thanks! –  Monty Nov 9 '12 at 23:51

Loop over them, make a new array with PID as key and QTY as value so you can add the latter up.

foreach ($array as $row) {
    list($pid, $qty) = $row;
    $sums[$pid] += $qty;
}

Obviously you could throw an isset in to suppress the notices.

And if you want your former array structure, convert it once more.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.