PHP Arrays Ex. #5: Associative Array
In this PHP exercise, you will again use the list of cities in Arrays Exercise #2. Here's the list, this time including countries as well as cities:
Tokyo, Japan; Mexico City, Mexico; New York City, USA; Mumbai, India; Seoul, Korea; Shanghai, China; Lagos, Nigeria; Buenos Aires, Argentina; Cairo, Egypt; London, England.
This time, create an associative array, using the countries as keys, the cities as values. Create a form for the user, with the instructions Please choose a city:
Follow this request with a select field for the 10 cities, with the options created by looping through the array. When the user clicks the submit button, return the statement $city is in $country., where $city is the value chosen by the user, and $country is its key.
 
 
Here's the code:
<!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" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1" />
<title>Associative Array - Cities</title>
</head>
<body>
<h2>Large Cities Again<br /></h2>
<?php
//Create associative array with countries as keys, cities as values.
$cities=array(
"Japan" => "Tokyo",
"Mexico" => "Mexico City",
"USA" => "New York City",
"India" => "Mumbai",
"Korea" => "Seoul",
"China" => "Shanghai",
"Nigeria" => "Lagos",
"Argentina" => "Buenos Aires",
"Egypt" => "Cairo",
"UK" => "London"
);
//If form not submitted, display form.
if(!isset($_POST['submit'])){
?>
<form method="post" action="yourfile.php">
<p>Please choose a city:</p>
<select name="city">
<?php
//Use array to create options for select field.
//Be sure to escape the quotes and include a line feed.
foreach($cities as $c){
echo "<option value=\"$c\">$c</option>\n";
}
?>
</select> <p />
<input type="submit" name="submit" value="Go">
</form>
<?php
//If form submitted, process input.
}else{
//Retrieve user response.
$city=$_POST['city'];
//Find corresponding key in associative array.
$country=array_search($city, $cities);
//Send the data back to the user.
echo "<p>$city is in $country.</p>" ;
}
?>
</body>
</html>
See the output of the script in a separate window here. You can also view the output's HTML source from the new window, if you need to check that.
Comments
Really like your website.
I Really like your website. It's nice to have exercises to do to test what you've learnt.
Here is my solution.
I really like your solution
I was stuck with attempting to pull out the key from the array. I tried several different functions to no avail, but your idea of flipping the array is brilliant.
Thank you a million!
(FYI: I attempted to create a function that could be reused to display the form, and ask users to type in the city name to get the Country they required, just for fun). Not sure I should post consider it's off on a tangent.
array_search not returning any data
I can only get array_search to return the key when I put in the literal text of the value field, e.g. London. It will not work if I substitute the $_POST variable, or the variable that I assign to the $_Post variable. My code is below, if you can help! Thank you.
<?php
?>
<?php
<?php
}
multiple selection capable
Here is my take for this exercise. You can select multiple entries, and all corresponding results will be displayed. It is short and simple.
<?php
}
}
}
?>