Use Ksort() To Sort By City Name.

<?php
$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";
?>

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.