Solved

Sum values of multi-dimensional array in PHP

Posted on 2014-01-10
7
1,631 Views
Last Modified: 2014-01-12
How would I calculate the sum of these two values inside this multi-dimensional array?

Array
(
    [Dec 01, 2013] => Array
        (
            [COM] => Array
                (
                    [C] => Array
                        (
                            [T] => 620.25
                            [U] => 24.75
                        )

                )

        )

)

Open in new window

0
Comment
Question by:DanielAttard
7 Comments
 
LVL 15

Expert Comment

by:gplana
ID: 39772276
if your array is on a variable called $a try this:

$sum = 0;
foreach($e1 in $a) {
   foreach($e2 in $e1){
      foreach($e3 in $e2){
         foreach($e4 in $e3){
            $sum += $e4;
         }
      }
   }
}
// here the $sum variable has the sum of the values.
0
 
LVL 15

Accepted Solution

by:
gplana earned 300 total points
ID: 39772285
Sorry, last code is not correct. Try this:

$sum = 0;
foreach($a as $e1) {
   foreach($e1 as $e2){
      foreach($e2 as $e3){
         foreach($e3 as $e4){
            $sum += $e4;
         }
      }
   }
}
// here the $sum variable has the sum of the values. 

Open in new window


You can see the foreach statement help here: http://www.php.net/manual/en/control-structures.foreach.php

Hope it helps. Regards!
0
 
LVL 31

Assisted Solution

by:Marco Gasi
Marco Gasi earned 200 total points
ID: 39772309
echo $array['Dec 01, 2013']['COM']['C']['T'] + $array['Dec 01, 2013']['COM']['C']['U'];

Open in new window

0
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.

 
LVL 15

Expert Comment

by:gplana
ID: 39772321
The marqusG solution is easier than mine, however my solution works for any array with 4 dimensions (as the array of your example).

MarqusG solution will only work on this specific example (with these values) while my solution will work for any 4-dimension array.

Hope it helps. Regards.
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39772429
Yes, it's true. Depending on your specific needs, one solution is better than the other: if you know  the array structure, the array keys and the exact position of the values you need to sum, my solution is more concise and less expensive in terms of resources since it doesn't require to loop through the whole array.

But if you need a more abstract and flexible routine, then gplana's solution is the one which fits you needs.

Reading the question, I have choosen the quicker solution. :)

Cheers
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39772612
See also the examples on the PHP man page:
http://php.net/manual/en/function.array-sum.php
0
 

Author Closing Comment

by:DanielAttard
ID: 39775288
Thanks to everyone for the comments.  I learned from all of them.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

920 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

16 Experts available now in Live!

Get 1:1 Help Now