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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:09:00+00:00 2026-06-15T22:09:00+00:00

I have two array which i want to merge in a specific way in

  • 0

I have two array which i want to merge in a specific way in php.
So i need your help in helping me with it as i tried and failed.

So say i have two arrays:

$array1= array(
    "foo"        => 3,
    "bar"        => 2,
    "random1"    => 4,
);

$array2= array(
    "random2"    => 3,
    "random3"    => 4,
    "foo"        => 6,
);

Now when during merging i would like the common key’s values to be added.
So like foo exists in array1 and in array2 so when merging array1 with array 2 i should get "foo" => "9"

I better illustration would be the final array which looks like this:

$array1= array(
    "foo"        => 9,
    "bar"        => 2,
    "random1"    => 4,
    "random2"    => 3,
    "random3"    => 4,
);

So again i would like the values of the common keys to be added together and non common keys to be added to array or a new array

I hope i was clear enough

Thanks,
Vidhu

  • 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-15T22:09:02+00:00Added an answer on June 15, 2026 at 10:09 pm

    Something like that:

    function mergeValues() {
        $result = array();
        $arraysToMerge = func_get_args();
        foreach ($arraysToMerge as $array) {
           foreach($array as $key => $value) {
               $result[$key] += $value;
           }
        }
    
        return $result;
    }
    
    $res = mergeValues($array1, $array2, $array3); // Can pass any ammount of arrays to a function.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two activities in which I need to pass an object array between
Hi I have two uneven JSON array which I am decoding in php. First
I have two array and three button in segment control i want that when
Possible Duplicate: What is the best method to merge two PHP objects? I have
I have an array of two dimensional Arrays. I want to create a new
I have two DIV-elements, which are assigned an array called stack using data() .
I have and array with two values and I want to use it with
I have two arrays which I merge using: $query = array_merge($query1, $query2); After merging
I have an array which appears like the one below. I want to slice
I have two arrays which contain objects of assets, now I want to subtract

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.