Solved

Sum values of multi-dimensional array in PHP

Posted on 2014-01-10
7
1,680 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
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.

 
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 109

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

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…
Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

808 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