funbinod Posted November 1, 2015 Share Posted November 1, 2015 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 More sharing options...
Ingolme Posted November 1, 2015 Share Posted November 1, 2015 On what line does that error appear? Link to comment Share on other sites More sharing options...
funbinod Posted November 2, 2015 Author Share Posted November 2, 2015 On what line does that error appear? echo $total Link to comment Share on other sites More sharing options...
justsomeguy Posted November 2, 2015 Share Posted November 2, 2015 $total is an array, what do you expect to happen when you echo it? Link to comment Share on other sites More sharing options...
funbinod Posted November 2, 2015 Author Share Posted November 2, 2015 $total is an array, what do you expect to happen when you echo it? i expect to get sum of all $balance that i get from query Link to comment Share on other sites More sharing options...
justsomeguy Posted November 2, 2015 Share Posted November 2, 2015 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 1 Link to comment Share on other sites More sharing options...
funbinod Posted November 6, 2015 Author Share Posted November 6, 2015 thank u for d reply and help. i got d required result using end($total) Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now