Solved

Sum values of multi-dimensional array in PHP

Posted on 2014-01-10
7
1,899 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
[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
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
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 
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 110

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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

726 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