Jump to content

Cannot get "foreach" to work from an array??


jhc1982

Recommended Posts

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!

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.