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.
 
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>Simple Array Loop - Cities</title>
</head>
<body>
<h2>Large Cities<br /></h2>
<?php
//Create array.
$cities=array(
"Tokyo",
"Mexico City",
"New York City",
"Mumbai",
"Seoul",
"Shanghai",
"Lagos",
"Buenos Aires",
"Cairo",
"London"
);
//Print values of array to browser, separated by commas.
foreach($cities as $c){
echo "$c, ";
}
//Sort array.
sort($cities);
//Print array as bulleted list.
echo "\n<ul>\n" ;
foreach($cities as $c){
echo "<li>$c</li>\n";
}
echo "</ul>" ;
//Add four cities to array.
array_push($cities, "Los Angeles", "Calcutta", "Osaka", "Beijing") ;
//Sort again, and print bulleted list.
sort($cities);
echo "\n<ul>\n";
foreach($cities as $c){
echo "<li>$c</li>\n";
}
echo "</ul>";
?>
</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.
To open a PHP code editor in a new tab, click here.
Comments
Sorting
Hello.
Whouldn't it have been better to sort using asort(), in order to preserve the key association?
The key association in this
The key association in this assignment really doesn't matter because it's not an associative array.
Comma
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:
Don't know if it's the best solution, but still looks better.
To do get rid of the comma, try this!
I don't think it looks too untidy. :P
}
Here's mine
<?php
//Determine the array size [alternative: count()]
//As first index key starts at 0, the last key is total size -1
}
}
}
?>
I did it my way :)
<?php
}
?>
<?php
}
?>
<?php
}
?>
Please chekck this one
<?php
{
}
}
?>
Here's my little code.
tried to keep it as short as possible but that "period" really called for an extra line of code.
<?php
}
?>
my code
solve comma problem using ternary operator
<?php
// comma (,)
}
// sort array
}
// add values to array
}