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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:09:50+00:00 2026-05-16T14:09:50+00:00

I actually am quite embarrassed to ask such a question but it is one

  • 0

I actually am quite embarrassed to ask such a question but it is one of those days that you spend 10 thousand hours on the simplest of functions and the more you try to solve them the more complicated a solution you get…. I don’t want to waste more time so here is the problem.

I have one array:

  $items=array(
    0=> array('name'=>'red','value'=>2),
    1=> array('name'=>'black','value'=>1),
    2=> array('name'=>'red','value'=>3)
  );

And I need a function that detects the identical names and merges them adding up their values. This means after the function finishes the array should look like this:

  $items=array(
    0=>array('name'=>'red','value'=>5),
    1=>array('name'=>'black','value'=>1)
  );

(‘red’ has two entries that have values 2 and 3, after the operation, red should have 1 entry with the value 5)

Thanks.

  • 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-05-16T14:09:51+00:00Added an answer on May 16, 2026 at 2:09 pm

    First off, can you simply make it an associative array so that it handles itself for you?

    $items = array(
        'red' => 5,
        'black' => 1,
    );
    

    If not, you could always do it by copying the array in a loop (not the best, but it works every time):

    $newItems = array();
    foreach ($items as $item) {
        if (!isset($newItems[$item['name']])) {
            $newItems[$item['name']] = $item;
        } else {
            $newItems[$item['name']]['value'] += $item['value'];
        }
    }
    $items = array_values($newItems);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code that was written without Tests but is actually quite
After years of ASP.NET development I'm actually quite surprised that I can't seem to
I actually happily design and develop Java EE Applications for quite 9 years, but
I need an XML-serializable dictionary. Actually, I now have two quite different programs that
Actually quite simple question: I've a python list like: ['1','2','3','4'] Just wondering how can
Yeah, the title can scare babies, but it's actually quite straightforward. I am trying
Ok, so this is actually quite a long story, but i'll try and keep
I'm testing some Jquery Ajax scripts and one of them is actually quite nice.
Before I explain the core issue, let me say that I'm actually quite interested
Actually what I want to get is quite simple, I want to get 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.