Follow:  Follow PHPExercises on Twitter  Follow us on Facebook

PHP Arrays Ex. #2: Simple Array Loop

For this exercise, you will use a list of ten of the largest cities in the world. (Please note, these are not the ten largest, just a selection of ten from the largest cities.) Create an array with the following values: Tokyo, Mexico City, New York City, Mumbai, Seoul, Shanghai, Lagos, Buenos Aires, Cairo, London.

Print these values to the browser separated by commas, using a loop to iterate over the array. Sort the array, then print the values to the browser in an unordered list, again using a loop.

Add the following cities to the array: Los Angeles, Calcutta, Osaka, Beijing. Sort the array again, and print it once more to the browser in an unordered list.

 

Comments

Hello.

Whouldn't it have been better to sort using asort(), in order to preserve the key association?

The key association in this assignment really doesn't matter because it's not an associative array.

Hi, thanks for the great site!

I'd like to make some esthetics addition.
The comma in the end of the first array print makes it look untidy. Nevertheless, putting If Statement in the Foreach Loop to check for the end of array seems to be untidy as well. So I think it's bit better this way:

foreach ($cities as $c) {
$txt .= "$c, ";
}
unset($c);
$txt=rtrim($txt,", ");
echo "$txt.";

Don't know if it's the best solution, but still looks better.

I don't think it looks too untidy. :P

  1. for ($i=0; $i<=9; $i++){
  2.     if ($i<9){echo $cities[$i].', ';}
  3.     else {echo $cities[$i];}
  4. }

  1. <?php
  2.  
  3. $cities = array('Tokyo', 'Mexico City', 'New York City', 'Mumbai', 'Seoul', 'Shanghai', 'Lagos', 'Buenos Aires', 'Cairo', 'London');
  4.  
  5. foreach ($cities as $key => $value) {
  6. 	//Determine the array size [alternative: count()]
  7. 	$last = sizeof($cities);	
  8. 	echo $value;
  9. 	//As first index key starts at 0, the last key is total size -1
  10. 	echo ($key == $last - 1) ? "." : ", ";
  11. }
  12.  
  13. echo "<br />";
  14.  
  15. sort($cities);
  16. echo "<ol>";
  17. foreach ($cities as $value) {
  18. 	echo "<li>$value</li>";
  19. }
  20. echo "</ol><br />";
  21.  
  22.  
  23. array_push($cities, 'Los Angeles', 'Calcutta', 'Osaka', 'Beijing');
  24. sort($cities);
  25. echo "<ul>";
  26. foreach ($cities as $value) {
  27. 	echo "<li>$value</li>";
  28. }
  29. echo "</ul><br />";
  30.  
  31. ?>

  1. <html xmlns="<a href="http://www.w3.org/1999/xhtml"">http://www.w3.org/1999/xhtml"</a> xml:lang="en" lang="en">
  2.  
  3. 	<head>
  4. 		<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  5. 		<title>Arrays Ex. #2</title>
  6. 	</head>
  7.  
  8. 	<body>
  9. 		<?php
  10. 		$cities = array("Tokyo", "Mexico City", "New York City", "Mumbai", "Seoul", "Shanghai", "Lagos", "Buenos Aires", "Cairo", "London");
  11. 		$countcities = count($cities);
  12. 		for($key = 0; $key < $countcities; $key++){
  13. 			echo " $cities[$key],";
  14. 		}
  15. 		echo "<br/>";
  16. 		sort($cities);
  17. 		?>
  18. 		<ul>
  19. 			<?php
  20. 			for($key = 0; $key < $countcities; $key++){
  21. 				echo "<li>$cities[$key]</li>";
  22. 			}
  23. 			?>
  24. 		</ul>
  25. 		<ul>
  26. 			<?php
  27. 			$cities1 = array("Los Angeles", "Calcutta", "Osaka", "Beijing");
  28. 			$cities2 = array_merge($cities, $cities1);
  29. 			sort($cities2);
  30. 			$countcities2 = count($cities2);
  31. 			for($key = 0; $key < $countcities2; $key++){
  32. 				echo "<li>$cities2[$key]</li>";
  33. 			}
  34. 			?>
  35. 		</ul>
  36. 	</body>
  37. </html>

  1. <?php
  2.  
  3. $cities=array("Tokyo", "Mexico City", "New York City", "Mumbai", 
  4.     "Seoul", "Shanghai", "Lagos", "Buenos Aires", "Cairo", "London");
  5.  
  6. asort($cities);
  7. foreach ($cities as $key => $value)
  8.     {
  9.  
  10.    echo  "<ul><li>$value,</li></ul>"; 
  11.  
  12.  
  13.  
  14. }
  15. echo"</br>";
  16.  
  17. array_push($cities, 'Los Angeles'," Calcutta", "Osaka", "Beijing");
  18. asort($cities);
  19.  
  20. foreach ($cities as $key => $value) {
  21.  
  22.     echo  "<ul><li>$value,</li></ul>"; 
  23.  
  24.  
  25.  
  26. }
  27. echo "<br />";
  28.  
  29.  
  30.  
  31.  
  32. ?>

tried to keep it as short as possible but that "period" really called for an extra line of code.

  1. <?php
  2. $city_names=array('Tokyo','Mexico City','New York City','Mumbai','Seoul','Shanghai','Lagos','Buenos Aires','Cairo','London');
  3. $strcount=count($city_names);
  4. foreach ($city_names AS $cn){
  5. 	echo "$cn";			
  6. 	if (($strcount-=1)==0) echo ".";
  7. 	else echo ", ";	
  8. 	}
  9. sort($city_names);
  10. foreach ($city_names AS $cn)
  11. 	echo "<li>$cn</li>";
  12.  
  13. echo"<br/>";
  14. array_push($city_names,'Los Angeles','Calcutta','Osaka','Beijing');
  15. sort($city_names);
  16. foreach ($city_names AS $cn)
  17. 	echo "<li>$cn</li>";
  18. ?>

solve comma problem using ternary operator

  1. <?php
  2.  
  3. $largestCities = array(
  4. 	'Tokyo',
  5. 	'Mexico',
  6. 	'New York',
  7. 	'Mumbai',
  8. 	'Seoul',
  9. 	'Shanghai',
  10. 	'Lagos',
  11. 	'Buenos Aires',
  12. 	'Cairo',
  13. 	'London',
  14. );
  15.  
  16. // comma (,)
  17. for ($i=0; $i < count($largestCities); $i++) {	
  18. 	echo $largestCities[$i] . (($i < count($largestCities) - 1) ? ', ' : '');
  19. }
  20.  
  21. // sort array
  22. sort($largestCities);
  23.  
  24. echo '<br />';
  25. echo '<ul>';
  26.  
  27. for ($i=0; $i < count($largestCities); $i++) {	
  28. 	echo '<li>' . $largestCities[$i] . '</li>';
  29. }
  30.  
  31. echo '</ul>';
  32.  
  33. // add values to array
  34. array_push($largestCities, 'Los Angeles', 'Calcutta', 'Osaka', 'Beijing');
  35. sort($largestCities);
  36.  
  37. echo '<br />';
  38. echo '<ul>';
  39.  
  40. for ($i=0; $i < count($largestCities); $i++) {	
  41. 	echo '<li>' . $largestCities[$i] . '</li>';
  42. }
  43.  
  44. echo '</ul>';