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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:33:56+00:00 2026-06-08T03:33:56+00:00

I have a loop that adds an array of two values to a main

  • 0

I have a loop that adds an array of two values to a main array.

How can I merge all of the arrays in the main array that have the same first values while at the same time add up the values of the second?

$mainData = array() ;
//Loop...

$cRes = $dbh->query("SELECT overdue FROM _credit_control_overdue WHERE entityID = $entityId") ;
        $currentOwed = $cRes->fetchColumn() ;
        $dbh->exec("REPLACE INTO _credit_control_overdue (entityID, overdue) VALUES ('$entityId', '$remaining')") ;
        $totalRemaining += $remaining ;

        array_push($mainData, array($entityId, $remaining)) ;
//End of loop

In many cases $entityId will be the same, and $remaining will be different.

Now I need a function similar to array_unique that will leave me with a unique $entityId but with all the $remaining values added up, so I am left with e.g. 2339, 83572.60.

Hope I have explained this clearly!


This is the output I desire:

Array ( [0] => Array ( [0] => 2499 [1] => 5314.50 ) [1] => Array ( [0] => 639 [1] => 75.00 ))

i.e Array ( [0] => UNIQUEID [1] => SUM )

  • 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-08T03:33:57+00:00Added an answer on June 8, 2026 at 3:33 am

    The best way to do this is probably to first build an associative array (hash) using the $entityId as the keys (which will give you a unique entry for each one) and the accumulated totals of $remaining as the values:

    // Initialise the hash before looping
    $perEntityTotals = array();
    
    /* Loop ... */
    
    // Add the $remaining value to the appropriate entity's total
    // If the key doesn't yet exist, it will be created with the value $remaining
    $perEntityTotals[$entityId] += $remaining;
    
    /* End Loop */
    

    To re-format this as you originally asked for, you’ll then need to manipulate the hash:

    $mainData = array();
    foreach ( $perEntityTotals as $entityId => $total )
    {
        $mainData[] = array($entityId, $total);
    }
    

    The below is equivalent, but somewhat harder to read:

    $mainData = array_map(
        NULL,             // see Example #4 on http://php.net/array_merge
        array_keys($perEntityTotals),
        array_values($perEntityTotals) 
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a loop that calculates the similarity between two documents. It collects all
I have the following loop that adds an object to an array if certain
I have a loop that adds elements to a 1d array: for i in
I have a function that adds shopping cart data to an array of arrays.
I have a loop that finds duplicate lines in a .ini file. I can
I have a loop that goes through all the news items we have on
I have a loop that can look like this: For Each article In artAll
I have an inline assembler loop that cumulatively adds elements from an int32 data
hey, this i have a loop that adds gathered strings and ints into an
I have two arrays in two MYSQL tables. I want to loop through 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.