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

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!

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.

@ 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

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.....

@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.

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

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

that is explode....

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.... :)

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

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

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 />";}
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..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.