Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8807965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:30:01+00:00 2026-06-14T02:30:01+00:00

I have an array that I need to process by adding the values, then

  • 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:30:02+00:00Added an answer on June 14, 2026 at 2:30 am

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just have the array of 15 values that all need to be inserted
I have a php array that has a bunch of data that I need
I have an array in javascript that I need to join and send through
I have following array, that I need to operate by hand on bitmaps. const
I have a file that I need to parse into an array but I
I need to get array of images that have to call from server and
I have a function that takes an array of objects and I need to
I have a method that takes an array of queries, and I need to
I have a PHP file that returns a JSON array. I need to extract
I have some inputs that I need to process from a form. The #

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.