0

ok so i am taking a list of numbers and stashing them i an array by going $total[]. then i try to use array_sum($total) and i dont get the last added number instead i get numbers that keep adding up to the final number
EX:
10,15,5
output: 102530

i have even just used $total += and i just get the same result. could some one help me with trying to just get the last total number.

thanks in advanced

5
Contributors
11
Replies
12
Views
7 Years
Discussion Span
Last Post by ashiqin
0

A clearer example might help. How exactly are you filling the array, how are you outputting the result, what result are you expecting, and what result do you get?
Example:

$total = array();
$total[] = 10; 
$total[] = 15; 
$total[] = 5; 
print ("sum is " . array_sum($total) )

This prints out "sum is 30", which is what I expected!

0

It looks like you're doing the summing inside of a loop. Try to wait to perform the sum calculation until after the loop has completed.

0

@ Wraithmanilian
thanks that is what the problem is. i am pretty good at php but i am still learning when it comes to somethings

thanks alot

0

u can use split....

like...

<?php
$str="10,15,5";

	    $parts = split('[,]', $str);
    print "First Is : $parts[0]<br>Second Is : $parts[1]<br>Third Is : $parts[2]<br><br>";

	$total=$parts[0]+$parts[1]+$parts[2];
	print "Total is : $total";
?>

Try this! this is what you actually want! it's good.....

0

@harshbavaliya - I ran that script you posted. Want to know what I expected that I got?

Deprecated: Function split() is deprecated

Using deprecated code in your scripts isn't exactly what I would call good. Right general idea, though.

0

actually it is not working in php 5.3....

but there is one another way to do this....

that is explode....

0

now run this script... It will work definitely....

<?php
$str="10,15,5";
 
	    $parts = explode(',', $str);
    print "First Is : $parts[0]<br>Second Is : $parts[1]<br>Third Is : $parts[2]<br><br>";
 
	$total=$parts[0]+$parts[1]+$parts[2];
	print "Total is : $total";
?>

The Problem Has Been Solved.... :)

Edited by harshbavaliya: just a new script...

0

haha problem was solved way before that. i just had to take the array sum out of the loop

0

k....
but it's a good way to code instead of looping. Because it will take some less time to code dear....

0

can someone help me with this code?..i need to calculate the sum of the array..but the sum is still doing in the loop..

while($row = mysql_fetch_array($result)){
$a = $row['COUNT(ratio)'];
    $result1 = sqrt ($a);
    $result2 += $result1; 
    $result3 = $result2;
    $result5 = $result4 / 2;


  echo "$result5 <br />";}

Edited by pyTony: fixed formatting

0
while($row = mysql_fetch_array($result)){
$a = $row['COUNT(ratio)'];
    $result1 = sqrt ($a);
    $result2 += $result1; 
    $result3 = $result2;
    $result5 = $result3 / 2;


  echo "$result5 <br />";}

this is my email..(snip)..can i get someone that expert in doing arithmetic equation using php..i really need to help me finish my foinal year project..tanx..

Edited by mike_2000_17: Fixed formatting

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.