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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:31:21+00:00 2026-06-05T14:31:21+00:00

How do I combine these two arrays, so that the keys stay the same,

  • 0

How do I combine these two arrays, so that the keys stay the same, but the values are arithmetically determined?

Note – the keys might not always line up in each array, per my example:

$Array1 = [4 => 100, 5 => 200, 6 => 100, 7 => 400];
$Array2 = [2 => 300, 5 => -100, 16 => -500];

Desired output:

$Array3 = [2 => 300, 4 => 100, 5 => 100, 6 => 100, 7 => 400, 16 => -500];
  • 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-05T14:31:23+00:00Added an answer on June 5, 2026 at 2:31 pm

    You can use array_map for this:

    $Array3 = array_map(function($a,$b) {return $a+$b;},$Array1,$Array2);
    

    However this will only work if you have the same keys in both arrays (which, in your example, you don’t).

    If this is an issue, the easiest workaround would probably be:

    $allKeys = array_merge(array_keys($Array1),array_keys($Array2));
    $Array3 = Array();
    foreach($allKeys as $k) {
        $Array3[$k] = (isset($Array1[$k]) ? $Array1[$k] : 0)
                     +(isset($Array2[$k]) ? $Array2[$k] : 0);
    }
    

    EDIT Just realised the above code is not optimal. Rewriting:

    $allKeys = array_unique(array_merge(array_keys($Array1),array_keys($Array2)));
    // rest of code as above
    

    Actually not sure if the overhead of repeated keys is more or less than the overhead of checking uniqueness…

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

Sidebar

Related Questions

I have two arrays of values that I would like to combine - but
I have two JavaScript arrays below that both have the same number of entries,
How do I combine these two select statements into one query: SELECT SUM( incidents
I want to combine these two queries into one: mysql_query(INSERT INTO categories (name, parent)
I have these 2 queries that I would like to combine into one big
I have two streams that i want to combine into one stream. The first
I have two arrays of objects that describe the structure of a neural network,
So I have been trying to combine the answers of these two questions: C#
I have two different databases that contain two tables with the exact same design.
I have two arrays (records returned from a database query) that I'm merging. I

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.