Use ksort() to sort by city name.
<?
$population = array('New York, NY' => 8008278,
'Los Angeles, CA' => 3694820,
'Chicago, IL' => 2896016,);
$total_population = 0;
ksort($population);
print "<table><tr><th>City</th><th>Population</th></tr>\n";
foreach ($population as $city => $people) {
$total_population += $people;
print "<tr><td>$city</td><td>$people</td></tr>\n";
}
print "<tr><td>Total</td><td>$total_population</td></tr>\n";
print "</table>\n";
?>
Related examples in the same category