Link to home
Start Free TrialLog in
Avatar of Neil_Bradley
Neil_BradleyFlag for New Zealand

asked on

array_sum

Hopefully a simple one..
How do I calculate the sum of $value?
foreach($mark as $ExamNo => $value) {
            
            if($value != "") {
                  echo '<li>'.$ExamNo.' = '.$value.'</li>';
                  echo "the sum of $value"
            }

      }
Thanks,
N
Avatar of Beverley Portlock
Beverley Portlock
Flag of United Kingdom of Great Britain and Northern Ireland image

If $mark is an array of values then

$totalMark = array_sum( $mark );

should do it. See http://www.php.net/array_sum
Avatar of Neil_Bradley

ASKER

No, I specifically need the sum of the $value inside the array
"I specifically need the sum of the $value inside the array"

I don't undertand what you mean by this. Do you mean
$sum_value = 0;

foreach($mark as $ExamNo => $value) {
            
            if($value != "") {
                  echo '<li>'.$ExamNo.' = '.$value.'</li>';

                  $sum_value += $value;
                  echo "the sum of $sum_value"
            }

      }

Open in new window

I have mocked up what I am trying to achieve here: http://www.statusconsultinglimited.com/questions.html
This should explain the aim of my question..
<?php
$mark = $_POST["mark"];

      foreach($mark as $ExamNo => $value) {
            
            if($value != "") {
                  echo '<li>'.$ExamNo.' = '.$value.'</li>';
            }

      }
echo"<br/>
You score was";
?>
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ray, as always you hit the nail on the head.
Thanks,
N
Thanks for that..
Cheers,
N