Solved

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

Posted on 2014-11-20
3
488 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 78

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Six Sigma Control Plans

729 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