0

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?

1

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.