You can save yourself a lot of headaches by studying the various built-in array functions at php.net. I have commented out some that will help you very much.
[php]<?php
$students = array (‘Tony’=>75,‘Maria’=>85, ‘Ray’=>77, ‘Carla’=>41, ‘Matthew’=>59, ‘Lana’=>99, ‘Greg’=>67, ‘Ariana’=>89,‘Denny’=>29, ‘Sulle’=>70);
echo ‘
’;
print_r($students);
echo ‘
’;
asort($students); // http://php.net/manual/en/function.arsort.php
echo ‘
’;
print_r($students);
echo ‘
’;
$sumGrd = array_sum($students); // http://php.net/manual/en/function.array-sum.php
$classSize = count($students);
$averages = $sumGrd / $classSize;
echo 'The average student grade is ’ . $averages . ‘%.
’;
foreach ( $students as $key => $value ) {
if ( $value < $averages ) {
echo 'This person ’ . $key . ’ fell below the class average of ’ . $averages . ‘% with a percent score of ’ . $value .’%
';
}
}[/php]
I apologize if I made a math error, while I’m good at math I’m no wizard. ;D
An in case you are wondering “What happens when my array gets a little more complicated” such as
[php]$school = array ( array( “student” => “John Jones”, “grade” => 99, “date” => “2014-10-31”), array( “student” => “Mary Jane”, “grade” => 39, “date” => “2014-10-31”));[/php]
http://php.net/manual/en/function.uasort.php -> this little function then would come in handy to use.