Skip to content

Instantly share code, notes, and snippets.

@amochohan
Created June 5, 2015 15:50
Show Gist options
  • Select an option

  • Save amochohan/41419d9896370c74be4e to your computer and use it in GitHub Desktop.

Select an option

Save amochohan/41419d9896370c74be4e to your computer and use it in GitHub Desktop.
PHP merge two arrays and sum the total of a common indexes
<?php
$balanceA=array(
"USD" => 10000,
"GBP" => 20000
);
$balanceB=array(
"USD" => 10000,
"GBP" => 20000,
"JPY" => 100
);
$sums = array();
foreach (array_keys($balanceA + $balanceB) as $currency) {
$sums[$currency] = (isset($balanceA[$currency]) ? $balanceA[$currency] : 0) + (isset($balanceB) ? $balanceB[$currency] : 0);
}
var_dump($sums);
@priana13
Copy link
Copy Markdown

priana13 commented Apr 7, 2020

thaks. Its work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment