Q695 Posted May 6, 2013 Share Posted May 6, 2013 Does anyone know how to total identical values in arrays easily? I tried the following, and none did what I needed: //echo "$sql_merge_stats<br>"; print_r($row_merge_stats); $total=array_merge($row_merge_stats); echo "<p>"; print_r($total); ///////////// echo "<p>"; $result_merge_stats = mysql_query($sql_merge_stats, $con) or die ("can not generate result " . mysql_error()); while ($row_merge_stats = mysql_fetch_assoc($result_merge_stats)){ print_r($row_merge_stats); echo "<br>"; ///////////////// $total=$total+$row_merge_stats; } echo "<br>"; print_r($total); //erroring $result_merge_stats = mysql_query($sql_merge_stats, $con) or die ("can not generate result " . mysql_error()); //$row_merge_stats = mysql_fetch_assoc($result_merge_stats); $sumArray = array(); foreach (mysql_fetch_assoc($result_merge_stats) as $k=>$subArray) { foreach ($subArray as $id=>$value) { $sumArray[$id]+=$value; } } print_r($sumArray); the output is: Array ( [__] => 1 [______] => 1 [___] => 1 [_______] => 1 ) Array ( [__] => 1 [______] => 1 [_____] => 1 [_____] => 1 ) Array ( [__] => 1 [______] => 1 [_____] => 1 [_____] => 1 ) Array ( [__] => 1 [_____] => 2 [_______] => 7 [_____] => 2 ) Array ( [__] => 1 [______] => 1 [______] => 1 [_____] => 1 ) Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted May 6, 2013 Share Posted May 6, 2013 Have you tried array_count_values()? http://php.net/manual/en/function.array-count-values.php Quote Link to comment Share on other sites More sharing options...
Q695 Posted May 6, 2013 Author Share Posted May 6, 2013 Have you tried array_count_values()? http://php.net/manual/en/function.array-count-values.php It would be like an accountant totaling columns, not simple counting. Quote Link to comment Share on other sites More sharing options...
Phear46 Posted May 6, 2013 Share Posted May 6, 2013 can you give some sample data and what the expected result from your sample would be. I have an idea but if its my idea its really simple and im proably misunderstanding! Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 6, 2013 Share Posted May 6, 2013 can you give some sample data and what the expected result from your sample would be. I have an idea but if its my idea its really simple and im proably misunderstanding! Better yet, you should provide some details about the DB schema and the query you are using. It would probably be more efficient to use the database for this than to post-process the DB results. Quote Link to comment Share on other sites More sharing options...
Q695 Posted May 6, 2013 Author Share Posted May 6, 2013 the output data for these would be:Array ( [__] => 1 [______] => 1 [_____] => 1 [_____] => 1 )Array ( [__] => 1 [_____] => 2 [_______] => 7 [_____] => 2 ) totals: 2 3 8 3 I forgot to pull the numbers for the corresponding arrays, but you get the picture. Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 6, 2013 Share Posted May 6, 2013 (edited) the output data for these would be: Array ( [__] => 1 [______] => 1 [_____] => 1 [_____] => 1 ) Array ( [__] => 1 [_____] => 2 [_______] => 7 [_____] => 2 ) totals: 2 3 8 3 I forgot to pull the numbers for the corresponding arrays, but you get the picture. Um, what? How are you relating those fields? If you had taken the time to put at least example names for the array indexes it would make some sense. But, what you have there makes no sense. How does an array field with an index of five underscores get added to the array index in another element of four underscores? Based upon what you were attempting previously foreach (mysql_fetch_assoc($result_merge_stats) as $k=>$subArray) { foreach ($subArray as $id=>$value) { $sumArray[$id]+=$value; } } You should do this in your query. Example SELECT SUM(field1) as field1total, SUM(field2) as field2total, SUM(field3) as field3total FROM tableName Edited May 6, 2013 by Psycho Quote Link to comment Share on other sites More sharing options...
Q695 Posted May 7, 2013 Author Share Posted May 7, 2013 How are you relating those fields? How does an array field with an index of five underscores get added to the array index in another element of four underscores? The fields are being pulled from a database. The totals are going top down, not left right, and if it was totaling left, right it would be easy to do, and I would put it on the right side of the array so you could logically draw the conclusion based on how math equations were written when you were a kid. The underscores are names made generic. Quote Link to comment Share on other sites More sharing options...
computermax2328 Posted May 7, 2013 Share Posted May 7, 2013 The fields are being pulled from a database. The totals are going top down, not left right, and if it was totaling left, right it would be easy to do, and I would put it on the right side of the array so you could logically draw the conclusion based on how math equations were written when you were a kid. The underscores are names made generic. I think that you are confused about what cyberRobot said. You can select the column you want, send it through a loop and use array_count_values() or count() to count all of the values in the column. Only loop the data in that column. Quote Link to comment Share on other sites More sharing options...
Q695 Posted May 7, 2013 Author Share Posted May 7, 2013 it's not counting, it's adding the numbers in the columns. Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 7, 2013 Share Posted May 7, 2013 The fields are being pulled from a database. The totals are going top down, not left right, and if it was totaling left, right it would be easy to do, and I would put it on the right side of the array so you could logically draw the conclusion based on how math equations were written when you were a kid. The underscores are names made generic. You obviously don't have an understanding of what the SUM() function in a query does - it sums the values across records (i.e. what you mean by top-down). The whole underscores only makes your example harder to understand since ____ and _____ aren't more different. If you had even looked at the sample query I provided and tried to use it I'm sure you would find it gives you the results you want in one simple query with no need to process the results. Quote Link to comment Share on other sites More sharing options...
Q695 Posted May 8, 2013 Author Share Posted May 8, 2013 I'm currently working on a project similar to D&D, how would summing an entry be useful there? Quote Link to comment Share on other sites More sharing options...
Q695 Posted May 8, 2013 Author Share Posted May 8, 2013 horizontal rows, not vertical rows. I think I'll just add each of the table lines in a loop. Quote Link to comment Share on other sites More sharing options...
Zane Posted May 8, 2013 Share Posted May 8, 2013 To sum horizontally, you just use + signs... just like math SELECT (col1 + col2 + col3+ col4) as total Quote Link to comment Share on other sites More sharing options...
Q695 Posted May 9, 2013 Author Share Posted May 9, 2013 To sum horizontally, you just use + signs... just like math SELECT (col1 + col2 + col3+ col4) as total $total=$total+$row_merge_stats; returns only returns the first row of stats. Quote Link to comment Share on other sites More sharing options...
Zane Posted May 9, 2013 Share Posted May 9, 2013 I'm referring to your SQL query not PHP. Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 9, 2013 Share Posted May 9, 2013 @Q695, I think it would help a great deal to show a true representative set of sample data and what you are trying to achieve. As I stated a long time back in this thread, I am almost certain you can get the data you need without doing any post processing of the data. If you need to sum the columns, then you would run a query such as the one I provided previously: SELECT SUM(field1) as field1total, SUM(field2) as field2total, SUM(field3) as field3total FROM tableName Or if you need a sum of multiple fields for each records (i.e. sum the row), then you would do something such as Zane provided SELECT (field1+field2+field3) as recordTotal FROM tableName Quote Link to comment Share on other sites More sharing options...
akphidelt2007 Posted May 9, 2013 Share Posted May 9, 2013 He probably wants to show the data also which is why he isn't using the SUM() in the query. He's basically trying to create a totals row at a bottom of a table. My guess is the way you are loop through the query. //this returns only the first result foreach (mysql_fetch_assoc($result_merge_stats) as $k=>$subArray) { foreach ($subArray as $id=>$value) { $sumArray[$id]+=$value; } } //this is what I'm guessing you want to do while($row = mysql_fetch_assoc($result_merge_stats)) { foreach($row as $id => $value) { $sumArray[$id]+=$value; } } Quote Link to comment Share on other sites More sharing options...
Solution Q695 Posted May 9, 2013 Author Solution Share Posted May 9, 2013 I'll do it the old way to make racking my mind easier. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.