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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:02:46+00:00 2026-06-16T07:02:46+00:00

Basically I need to take two arrays, merge them with unique values and sum

  • 0

Basically I need to take two arrays, merge them with unique values and sum one of columns. It makes more sense when written out below:

$a = [
    ['ID' => 1, 'Count' => 2],
];

$b = [
    ['ID' => 1, 'Count' => 4],
    ['ID' => 2, 'Count' => 3]
];

and I need the final product to be:

$a_plus_b = [
    ['ID' => 1, 'Count' => 6],
    ['ID' => 2, 'Count' => 3]
];

I have been playing with different variations of array_merge() and array_unique(), but I can’t find an efficient way to do what I need. I know I can always do nested loops, but I was hoping for something easier. Any ideas?

  • 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-16T07:02:47+00:00Added an answer on June 16, 2026 at 7:02 am

    This should do the trick

    Note: This solution requires PHP >= 5.3. There is a PHP < 5.3 solution below.

    $input = array($a, $b);
    // add as many result arrays to $input as you want; e.g.,
    // $input = array($a, $b, $c, $d);
    
    $output = array_count_values(
      call_user_func_array(
        'array_merge',
         array_map(
           function($arr) {
             return array_fill(0, $arr['Count'], $arr['ID']);
           },
           call_user_func_array(
             'array_merge',
             $input
           )
         )
      )
    );
    
    print_r($output);
    

    Output

    Array
    (
        [1] => 6
        [2] => 3
    )
    

    Note the array keys above are ID values. The array values are Count values.


    If you’re running PHP < 5.2 you won’t be able to use the inline closure with array_fill. You have to define it as a separate function.

    $input = array($a, $b);
    
    function _fill($arr) {
      return array_fill(0, $arr['Count'], $arr['ID']);
    }
    
    $output = array_count_values(
      call_user_func_array(
        'array_merge',
        array_map(
          '_fill',
          call_user_func_array(
            'array_merge',
            $input
          )
        )
      )
    );
    
    print_r($output);
    

    From here, converting the output to your desired format is a trivial task.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically I need to merge two arrays where the value 0 will take priority
basically I need a loop within a loop to compare two different arrays in
I need to know how to take 10.25 and turn it to 1025 basically
I'm trying to take two doubles (GPS coordinates) and send them over the ZigBee
I need to take the output of $one and make an array, however, the
I need to create an image badge in Cocoa. Basically I need to take
Basically need to generate custom(some different then yes no) messeges(alert) in JS , how
basically need to change the value that - admin_url() returns any idea?
I basically need to get user input: gets.chomp(input?) And then to convert the given
I basically need a custom function to be used only when, for example, 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.