webguync Posted November 20, 2011 Share Posted November 20, 2011 I almost have this right, but not quite. I am trying to echo out the average score if a person scored above 70 and my averaging isn't correct. It should echo out 89, but instead the number is 63.25. The averaging is the only part not working. Everything else works as intended. I might not have my curly braces in the proper order or something. I think it's something stupid I am forgetting. Here is my code. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Array Test</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <?php function array_average_nonzero($scores) { return array_sum($scores) / count(array_filter($scores)); } $scores["Tim"] = 98; $scores["Tom"] = 80; $scores["Mike"] = 50; $scores["Jason"] = 25; foreach( $scores as $key => $value) if($value < 70){ echo "$key scored a $value which is less than 70. <br />\n"; } elseif ($value < 70); { echo "The average score of those who scored over 70 is: " .array_sum($scores) / count($scores) . "<br />"; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/251501-need-help-averaging-array-integers-within-if-else-block/ Share on other sites More sharing options...
Alexv Posted November 20, 2011 Share Posted November 20, 2011 You need to remove the values that are < 70. Add this line after that first echo. unset($scores[$key]); Quote Link to comment https://forums.phpfreaks.com/topic/251501-need-help-averaging-array-integers-within-if-else-block/#findComment-1289832 Share on other sites More sharing options...
webguync Posted November 20, 2011 Author Share Posted November 20, 2011 that was easy enough, problem solved, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/251501-need-help-averaging-array-integers-within-if-else-block/#findComment-1289835 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.