Follow:  Follow PHPExercises on Twitter  Follow us on Facebook

PHP Arrays Ex. #5: Associative Array

Ex6

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.

 

Comments

I Really like your website. It's nice to have exercises to do to test what you've learnt.

Here is my solution.


<h2>Associative Array</h2>
<?php

//create associative array
$cities = array(
	'Japan'=>'Tokyo',
	'Mexico'=>'Mexico City',
	'USA'=>'New York City', 
	'India'=>'Mumbai',
	'Korea'=>'Seoul',
	'China'=>'Shanghai',
	'Nigeria'=>'Lagos', 
	'Argentina'=>'Buenos Aires', 
	'Egypt'=>'Cairo', 
	'England'=>'London' 
	);
	if(!isset($_POST['submit'])){	
?>

<form method="post" action="arraystwo.php">
	<p>
	Please choose a city:
	<select name="city" size="1" >
		<?php
		//loop through array to create options
		foreach($cities as $x){
		echo "$x";
		}
		?>
	</select>
	</p>
	<p><input type="submit" name="submit" value="Send" /></p>
</form>
<?php
}
else{
//Get user selection
$city = $_POST['city'];
//Flip array so the keys become the value
$country = array_flip($cities);

echo "<p>$city is in $country[$city]</p>";

}
?>

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.

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.

  1.  <?php
  2.  
  3. $places = array(
  4. 	"Japan" => "Tokyo",
  5.     "Mexico" => "Mexico City",
  6.     "USA" => "New York City",
  7.     "India" => "Mumbai",
  8.     "Korea" => "Seoul",
  9.     "China" => "Shanghai",
  10.     "Nigeria" => "Lagos",
  11.     "Argentina" => "Buenos Aires",
  12.     "Egypt" => "Cairo",
  13.     "UK" => "London");
  14.  
  15.  
  16. ?>
  17.  
  18. Please pick a city:
  19. <form action="" method="post">
  20. 	<select name="city">
  21. <?php
  22. 	foreach ($places as $key => $value) {
  23. ?>	<option value=" <?php echo $value ; ?>"><?php echo $value;
  24. }?>
  25. 	</select>
  26. 	<input type="submit">
  27. </form>
  28.  
  29.  
  30. <?php
  31.  
  32. if(isset($_POST['city'])) {
  33.  
  34. 		$ciudad = $_POST['city'];
  35. 		$country = array_search($_POST['city'], $places);
  36. 		echo "You have chosen" . $_POST['city'] . ", which is in" . $country;
  37.  
  38.  
  39.  
  40.  
  41.  
  42. } else {
  43. 	echo "no";
  44. }

Here is my take for this exercise. You can select multiple entries, and all corresponding results will be displayed. It is short and simple.

  1. <html>
  2. <head><title>Select Field Exercise - Joe Play</title></head>
  3. <body>
  4. 	<?php		
  5. 		$places=array('Japan'=>'Tokyo','Mexico'=>'Mexico City','USA'=>'New York City','India'=>'Mumbai',
  6. 		'South Korea'=>'Seoul','China'=>'Shanghai','Nigeria'=>'Lagos','Argentina'=>'Buenos Aires',
  7. 		'Egypt'=>'Cairo','England'=>'London');
  8.  
  9. 		if (!empty($_POST["selectedPlace"])) {
  10. 			foreach($_POST["selectedPlace"] as $selectedCity){
  11. 				foreach($places as $country => $city)
  12. 					if($city==$selectedCity) echo"$city is in $country.<br/>";
  13. 			}
  14. 		}		
  15. 		echo"<br/>Please choose a city: <br/>(Press and hold CTRL key for multiple selection)<br/>";		
  16. 		echo"<form action='' method='post'>";
  17. 			echo"<select name='selectedPlace[]' multiple='yes' size='".count($places)."'>";
  18. 				foreach($places as $country => $city){
  19. 					echo"<option value=\"$city\">$city</option>";
  20. 				}
  21. 			echo"</select>";
  22. 			echo"<br/><input type='submit' value='Check it!' />";
  23. 		echo "</form>";
  24. 	?>							
  25. </body>
  26. </html>