0

I want all of the elements in the array to be added together, but this doesn't seem to be working.

    <?php

    function mimic_array_sum($array) {
        foreach($array as $total) {
            $total = $total + $total;
        }
        return $total;
    }
    $var = array(1,2,3,4,5);
    $total = mimic_array_sum($var);
    echo $total;
?>
3
  • What does "doesn't seem to be working" mean? What results are you expecting? The #1 problem you have is that you're using $total as both a loop variable and an accumulator. Commented Sep 7, 2013 at 17:39
  • 1
    You're overwriting your $total variable on each iteration Commented Sep 7, 2013 at 17:39
  • Related: Php foreach- Why it doesn't override the array value while iterating? Commented Aug 15, 2023 at 22:18

6 Answers 6

2

$total = $total + $total --> well, there's your problem...

The $total variable gets overwritten on each loop through the array. Assign a separate variable for each number in the array, like so:

function mimic_array_sum($array) {
    $total = 0;
    foreach($array as $number) {
        $total = $total + $number;
    }
    return $total;
}
$var = array(1,2,3,4,5);
echo mimic_array_sum($var);

Although the point of this is not clear to me... You might as well use the php-function array_sum...

$var = array(1,2,3,4,5);
echo array_sum($var);
Sign up to request clarification or add additional context in comments.

Comments

1
$var = array(1,2,3,4,5);
$total = array_reduce(
    $var,
    function($sum, $value) {
        return $sum + $value;
    }
);

though why not simply use array_sum()?

Comments

1

You can use array_sum — Calculate the sum of values in an array

$var = array(1,2,3,4,5);
$total = array_sum($var);
echo $total;

Comments

0
  <?php

    function mimic_array_sum($array) {
            $total = 0;
        foreach($array as $elem) {
            $total += is_numeric($elem) ? $elem : 0;
        }
        return $total;
    }
    $var = array(1,2,3,4,5);
    $total = mimic_array_sum($var);
    echo $total;
?>

Comments

0
<?php
$a = array(1,2,3,4,5);
echo "sum is:".array_sum($a);
?>

See Manual

4 Comments

This wouldn't work. It'll just output some Array to String conversion errors.
@AmalMurali Thanks buddy I forget to write array_sum even I have attached the correct manual link :P
Yeah, should work now, but doesn't actually answer the OP's question though.
Focus on the title add numbers in an array with PHP
0

Please try following, you have to take separate variable to do so. Or else you can use array_sum()

function mimic_array_sum($array) {
    $test = 0;
    foreach($array as $total) {
        $test = intval($test) + intval($total);
    }
    return $test;
}
$var = array(1,2,3,4,5);
$total = mimic_array_sum($var);
echo $total;

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.