Solved

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

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

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

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
The viewer will learn how to count occurrences of each item in an array.
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…

760 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

19 Experts available now in Live!

Get 1:1 Help Now