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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:37:55+00:00 2026-05-29T05:37:55+00:00

I have an array with keys and values. Each value is an integer. I

  • 0

I have an array with keys and values. Each value is an integer. I have an other array with the same keys. How can I subtract all of the values for the matching keys? Also there might be keys that do not occur in the second array but both arrays have the same length. If there is a key in array 2 that is not present in array 1 its value should be unchanged. If there is a key in the first array that is not in the second it should be thrown away. How do I do it? Is there any built-in function for this?

If I would write a script it would be some kind of for loop like this:

$arr1 = array('a' => 1, 'b' => 3, 'c' => 10);
$arr2 = array('a' => 2, 'b' => 1, 'c' => 5);
$ret = array();
foreach ($arr1 as $key => $value) {
    $ret[$key] = $arr2[$key] - $arr1[$key];
}
print_r($ret);
/*
should be: array('a' => 1, 'b' => -2, 'c' => -5)
*/

I did not add the occasion here a key is in one array and not in the other.

  • 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-29T05:37:55+00:00Added an answer on May 29, 2026 at 5:37 am

    You could avoid the foreach using array functions if you were so inclined.

    The closure provided to array_mapdocs below will subtract each $arr1 value from each corresponding $arr2. Unfortunately array_map won’t preserve your keys when using more than one input array, so we use array_combinedocs to merge the subtracted results back into an array with the original keys:

    $arr1 = array('a' => 1, 'b' => 3, 'c' => 10);
    $arr2 = array('a' => 2, 'b' => 1, 'c' => 5);
    
    $subtracted = array_map(function ($x, $y) { return $y-$x; } , $arr1, $arr2);
    $result     = array_combine(array_keys($arr1), $subtracted);
    
    var_dump($result);
    

    UPDATE

    I was interested in how the array functions approach compared to a simple foreach, so I benchmarked both using Xdebug. Here’s the test code:

    $arr1 = array('a' => 1, 'b' => 3, 'c' => 10);
    $arr2 = array('a' => 2, 'b' => 1, 'c' => 5);
    
    function arrayFunc($arr1, $arr2) {
      $subtracted = array_map(function ($x, $y) { return $y-$x; } , $arr1, $arr2);
      $result     = array_combine(array_keys($arr1), $subtracted);
    }
    
    function foreachFunc($arr1, $arr2) {
      $ret = array();
      foreach ($arr1 as $key => $value) {
        $ret[$key] = $arr2[$key] - $arr1[$key];
      }
    }
    
    for ($i=0;$i<10000;$i++) { arrayFunc($arr1, $arr2); }
    for ($i=0;$i<10000;$i++) { foreachFunc($arr1, $arr2); }
    

    As it turns out, using the foreach loop is an order of magnitude faster than accomplishing the same task using array functions. As you can see from the below KCachegrind callee image, the array function method required nearly 80% of the processing time in the above code, while the foreach function required less than 5%.

    enter image description here

    The lesson here: sometimes the more semantic array functions (surprisingly?) can be inferior performance-wise to a good old fashioned loop in PHP. Of course, you should always choose the option that is more readable/semantic; micro-optimizations like this aren’t justified if they make the code more difficult to understand six months down the road.

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

Sidebar

Related Questions

I have a PHP 2D array, many keys, each with one value, I need
I have an array of dictionaries with each a bunch of keys with values.
I have an array that has keys and values. For eg: Array ( [name]
I have two separate arrays - $array_before and $array_after . Each array can have
You can't use Enumerable#map to look up the same value from each element an
I have an array of all possible combinations of values, a bit like working
I have an NSArray of NSDictionaries where each dictionary has the same keys. I
I have been having this issue in iterating through an array of keys and
So, I have an associative array and the keys in the array are properties
I have array of select tag. <select id='uniqueID' name=status> <option value=1>Present</option> <option value=2>Absent</option> </select>

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.