Solved

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

Posted on 2014-11-20
3
438 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

825 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