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 8122503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:51:25+00:00 2026-06-06T05:51:25+00:00

Well, I must say , I am lost with a feeling that the operation

  • 0

Well, I must say , I am lost with a feeling that the operation should be fast enough so looking for a fast performing combine.

my array Looks like

Array
(
    [0] => Array
        (
            [id] => 35
            [item] => Ball Pen
            [qty] => 1
            [price] => 23
            [total] => 23
        )

    [1] => Array
        (
            [id] => 34
            [item] => Summer vest
            [qty] => 1
            [price] => 23
            [total] => 23
        )

    [2] => Array
        (
            [id] => 34
            [item] => Summer vest
            [qty] => 3
            [price] => 23
            [total] => 69
        )
)

And as output I want a sum of qty if the id is occurring more than once

Array
(
    [35] => Array
        (
            [id] => 35
            [item] => Ball Pen
            [qty] => 1
            [price] => 23
            [total] => 23
        )

    [34] => Array
        (
            [id] => 34
            [item] => Summer vest
            [qty] => 4
            [price] => 46
            [total] => 92
        )

)
  • 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-06T05:51:27+00:00Added an answer on June 6, 2026 at 5:51 am

    Something like this should work

    $sum = array();
    $input = array(); // Your array
    array_walk( $input, function( $el) use( &$sum) {
        if( !isset( $sum[ $el['id'] ] ))
            $sum[ $el['id'] ] = 0;
        $sum[ $el['id'] ] += $el['qty'];
    });
    

    Here is a demo showing the correct output.

    array(2) { [35]=> int(1) [34]=> int(4) } 
    

    You can get the updated output with something like this:

    $output = array();
    $input = array(); // Your array
    array_walk( $input, function( $el) use( &$output) {
        if( !isset( $output[ $el['id'] ] ))
            $output[ $el['id'] ] = array( 
                'id' => $el['id'], 
                'item' => $el['item'], 
                'qty' => 0, 
                'price' => 0, 
                'total' => 0
            );
    
        $output[ $el['id'] ]['qty'] += $el['qty'];
        $output[ $el['id'] ]['price'] += $el['price'];
        $output[ $el['id'] ]['total'] += $el['total'];
    });
    var_dump( $output);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I'm designing af tool that must screenshot well for printed documentation, can I
First of all, I must say that I am quite new to the API
I know SQL well but I must be missing something really dumb here. This
Well, I must be brain-damaged, because I can't find the java source for Sun's
Well, first of all sorry about this question it must be pretty straight forward
As the title states, I am looking for something, that will help me automate
Well, lets say I'm building ActiveMQ based chat application. It's pretty simple. Having only
Let's say I have an array like this: $array = array ('1','2','3','4'); And I
Let say you are working on a website template that has many pages (index.html,
I've been learning Python and I must say I loved it. But as a

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.