?
Solved

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

Posted on 2014-11-20
3
Medium Priority
?
513 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 79

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 2000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
The viewer will learn how to count occurrences of each item in an array.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

752 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