I have 2 arrays
Array
(
[010156] => Array
(
[supp_number] => 010156
[totalamount] => 4.113,23
[debtorcred] => H
[amount1] => 4.113,23-
[amount2] =>
[amount3] =>
[amount4] =>
[amount5] =>
[amount6] =>
)
)
Array
(
[010156] => Array
(
[supp_number] => 010156
[totalamount] => 4.113,23
[debtorcred] => H
[amount1] => 4.113,23-
[amount2] =>
[amount3] =>
[amount4] =>
[amount5] =>
[amount6] =>
)
)
Is it possible that I can combine these separate arrays into one and add the values together so that the outcome will be:
Array
(
[010156] => Array
(
[supp_number] => <<<TOTAL >>>
[totalamount] => <<<TOTAL >>>
[debtorcred] => <<<TOTAL >>>
[amount1] => <<<TOTAL >>>
[amount2] => <<<TOTAL >>>
[amount3] => <<<TOTAL >>>
[amount4] => <<<TOTAL >>>
[amount5] => <<<TOTAL >>>
[amount6] => <<<TOTAL >>>
)
)
This is the function I have at the moment but I cannot seem to make it work:
function array_merge_numeric_values()
{
$arrays = func_get_args();
$merged = array();
foreach ($arrays as $array)
{
foreach ($array as $key => $value)
{
if ( ! isset($merged[$key]))
{
$merged[$key] = $value;
}
else
{
$merged[$key] += $value;
}
}
}
This actual calculation can be done in 2 lines of code, no need for loops:
http://codepad.viper-7.com/ieSkHQ
Youl’ll just need to make the appropriate adjustment to account for you nested structure.