Jump to content

get sum of all result


funbinod

Recommended Posts

i want to sum each value from the retrieved values from an array. but i'm failing in this

 

like, i get $balance for each $aid

$ttl = 0while ($r = $q->fetch_array) {    // ..................    // ..................    $aid = $r['aid'];    $balance = $r['balance'];}$ttl += $balance;$totamtarr[ $aid ] = $balance;$runSum = 0;foreach($totamtarr as $clamt) {    $runSum += $clamt;    $total[] = $runSum;}echo $total;

but this $total say -> "Array to string conversion in.................."

 

please guide.

 

thanks in advance...

Link to comment
Share on other sites

It's an array of values though. PHP doesn't have a rule that if you print an array of numbers it's going to add all the numbers up and print that. It's just an array. If you want to print the sum of the numbers then there's a function for that:http://php.net/manual/en/function.array-sum.php

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...