Solved

Sum the values of two associative multi-dimensional arrays in PHP

Posted on 2014-11-20
3
431 Views
Last Modified: 2014-11-20
I am trying to sum the values of two associative arrays. This is the first array:

Array
(
[Jan 01, 2013] => Array
    (
        [COM] => 100
        [RES] => 200
    )

[Oct 28, 2014] => Array
    (
        [COM] => 300
        [RES] => 400
    )
)

Open in new window

and this is the second array:

Array
(
[Jan 01, 2013] => Array
    (
        [COM] => 10
        [RES] => 20
    )

[Oct 28, 2014] => Array
    (
        [COM] => 30
        [RES] => 40
    )
)

Open in new window

I need to sum the values from these two arrays so that it produces this result:

    Array
(
[Jan 01, 2013] => Array
    (
        [COM] => 110
        [RES] => 220
    )

[Oct 28, 2014] => Array
    (
        [COM] => 330
        [RES] => 440
    )
)

Open in new window

I found this tutorial to sum values of associative arrays, but it does not seem to work with my multi-dimensional arrays. Any suggestions how to accomplish this? Thank you.
0
Comment
Question by:DanielAttard
3 Comments
 
LVL 77

Expert Comment

by:arnold
ID: 40456621
You can use foreach $element in (@array) {
Then use the dereferencing to cycle through the second level in a similar way
$array[$elelemnt]
You would need to use exists test to avoid error of trying to add null values.

Are you using the storable module to store this data in a file that you then load/writeout?
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 40456623
change $sum = $in[$key] + $out[$key]; in the tutorial to
  if( is_array($in[$key]) ){
        $sum = array_mesh($in[$key], $out[$key]);
  }else{
        $sum = $in[$key] + $out[$key];
  }
0
 

Author Closing Comment

by:DanielAttard
ID: 40456682
Great stuff @ozo, this worked like a charm.  More importantly, I learned a few things in the process.  Thanks.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Read about why website design really matters in today's demanding market.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now