Solved

Sum values of multi-dimensional array in PHP

Posted on 2014-01-10
7
1,578 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 30

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 30

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

757 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

22 Experts available now in Live!

Get 1:1 Help Now