Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

why is my array_sum showing the wrong number?

I have this array and I want to add the values:

$sustainCapital_arr = Array ( [0] => 2,759 [1] => 3,269 [2] => 3,481 [3] => 3,573 [4] => 3,997 [5] => 4,421 [6] => 10,999 )

now, interestingly array_sum is giving me an incorrect number:

$total_Sustaining = array_sum($sustainCapital_arr);     output: 28 ???

Output should be 32,499.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Now, I also tried to use a foreach loop and the same happens. What the hell is going on here?

$total_Sustaining = 0;
foreach ($sustainCapital_arr as $key=>$value){
                            
   $total_Sustaining += $value;
}

output is again 28!!!

What am I doing wrong here?

>Solution :

$total_Sustaining = 0;
foreach ($sustainCapital_arr as $key=>$value){
                            
   $total_Sustaining += filter_var($value, FILTER_SANITIZE_NUMBER_INT);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading