jhc1982 Posted December 14, 2010 Share Posted December 14, 2010 Hi everyone, I am just starting out with php so I am sure this is a really silly question. I have been working through php & mySQL for dummies (latest edition) and each time I read a new chapter I try to implement the tutorial. I have been trying something really simple from the book and have now even tried copying it word for word but it still will not work. I am basically trying to output a list of Cities and States with the following code: <?php $capitals = array ("CA" => "Sacramento", "TX" => "Austin", "OR" => "Salem" ); foreach ($capitals as $state => $city); ksort ($capitals); { echo "$city, $state<br />"; } ?> Instead of getting: Sacramento, CA Salem, OR Austin, TX I just get: Salem, OR I am running XAMPP on Mac OS X 10.6.5. Any ideas or help would be awesome! Quote Link to comment https://forums.phpfreaks.com/topic/221573-cannot-get-foreach-to-work-from-an-array/ Share on other sites More sharing options...
trq Posted December 14, 2010 Share Posted December 14, 2010 Move ksort out of your loop. Quote Link to comment https://forums.phpfreaks.com/topic/221573-cannot-get-foreach-to-work-from-an-array/#findComment-1146965 Share on other sites More sharing options...
jhc1982 Posted December 14, 2010 Author Share Posted December 14, 2010 Thanks for the quick reply thorpe. I tried this and it didn't work. Could I have maybe installed XAMPP incorrectly? I just followed its install instructions. Quote Link to comment https://forums.phpfreaks.com/topic/221573-cannot-get-foreach-to-work-from-an-array/#findComment-1146967 Share on other sites More sharing options...
trq Posted December 14, 2010 Share Posted December 14, 2010 Can you post your actual code? The code you have posted would produce nothing but syntax errors. Quote Link to comment https://forums.phpfreaks.com/topic/221573-cannot-get-foreach-to-work-from-an-array/#findComment-1146983 Share on other sites More sharing options...
jhc1982 Posted December 14, 2010 Author Share Posted December 14, 2010 Hi Thorpe, This is the code as copied from the book in an html page: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Untitled Document</title> </head> <body> <?php $capitals = array ("CA" => "Sacramento", "TX" => "Austin", "OR" => "Salem" ); ksort ($capitals); foreach ($capitals as $state => $city); { echo "$city, $state<br />"; } ?> </body> </html> I now just get: Austin, TX I have a feeling I may have really missed the thread! Quote Link to comment https://forums.phpfreaks.com/topic/221573-cannot-get-foreach-to-work-from-an-array/#findComment-1146991 Share on other sites More sharing options...
trq Posted December 14, 2010 Share Posted December 14, 2010 Again, that should produce nothing but a syntax error. Remove the ; at the end of your foreach() line. foreach ($capitals as $state => $city) { echo "$city, $state<br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/221573-cannot-get-foreach-to-work-from-an-array/#findComment-1146999 Share on other sites More sharing options...
jhc1982 Posted December 14, 2010 Author Share Posted December 14, 2010 Thanks loads thorpe! I went a bit crazy on the old ; I can't believe I missed that. Really appreciate your help! Quote Link to comment https://forums.phpfreaks.com/topic/221573-cannot-get-foreach-to-work-from-an-array/#findComment-1147001 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.