ashez2ashes Posted January 25, 2012 Share Posted January 25, 2012 I'm stuck. I'm on chapter 7.5 sorting. I've checked it over and over and I can't find anything different from what I typed from the book's code. But it keeps coming up like this: I also tried emptying my internet cache and restarting but that didn't help. I tried on XAMPP and on my godaddy hostserver and got the same results. Here's the code I typed. ------------ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" xml:lang="en" lang="en"/> <title>My Little Gradebook</title> </head> <body> <?php // Script 7.5 - sort.php //Create the initial array $grades = array ( 'Richard' => 95, 'Sherwood' => 82, 'Toni' => 98, 'Franz' => 87, 'Melissa' => 75, 'Roddy' => 85, ); //Show what the array looked like before you sort it print '<p>Originally the array looked like this: <br />'; foreach ($grades as $student => $grade) { print "$student: $grade<br />\n"; } print "</p>"; //Sort and print the array in reverese order by value to show who has the highest grade arsort ($grades); print '<p>After sorting the array by value using arsort(), the array looks like this:<br />'; foreach ($grades as $students => $grade) { print "$student: $grade<br />\n"; } print "</p>"; //Sort the array by key to put them in alphabetical order by name ksort($grades); print '<p>After sorting the array by key using ksort(), the array looks like this:<br />'; foreach ($grades as $students => $grade) { print "$student: $grade<br />\n"; } print "</p>"; ?> </body> </html> ------------- Link to comment Share on other sites More sharing options...
ashez2ashes Posted January 25, 2012 Author Share Posted January 25, 2012 Nevermind I feel dumb. I typed student instead of students. I'm surprised it repeated that one name instead of showing nothing or giving an error message. Weird. Link to comment Share on other sites More sharing options...
Larry Posted January 28, 2012 Share Posted January 28, 2012 Ah, good catch. I missed that the first time myself. Kudos for figuring it out and thanks for sharing your solution. The reason is always printed out the one name is that the first loop uses $student. The last item in that loop has a $student value of Roddy. Since $student doesn't get assigned any other values, it still has that value. Link to comment Share on other sites More sharing options...
Recommended Posts