?
Solved

Sum values of multi-dimensional array in PHP

Posted on 2014-01-10
7
Medium Priority
?
2,429 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 1200 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 800 total points
ID: 39772309
echo $array['Dec 01, 2013']['COM']['C']['T'] + $array['Dec 01, 2013']['COM']['C']['U'];

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 111

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
The first step to building an amazing About page is to figure out what you want the page to say about your company. You then must grab the attention of the reader, boast a bit, tell a story and let others brag about you. With a little bit of thought…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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.
Suggested Courses
Course of the Month15 days, 3 hours left to enroll

839 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