# PHP Tutorial :: Exercises (III)

## PHP Exercise #7

### Data table built with an array

According to the US census from 2000, the 10 most populated cities of United States were: New York [NY] (8.008.278), Los Angeles [CA] (3.694.820), Chicago [IL] (2.896.016), Houston [TX] (1.953.631), Philadelphia [PA] (1.517.550), Phoenix [AZ] (1.321.045), San Diego [CA] (1.223.400), Dallas [TX] (1.188.580), San Antonio [TX] (1.144.646), Detroit [MI] (951.270). Define an array that contains this information about cities and populations and print a table that shows that information and the total population in the 10 cities. Print two tables; one in which cities are ordered by their name, and other in which they are ordered by population, from smaller to larger (ascending order).

<?php
\$population = array('New York, NY' => 8008278,
'Los Angeles, CA' => 3694820,
'Chicago, IL' => 2896016,
'Houston, TX' => 1953631,
'Phoenix, AZ' => 1321045,
'San Diego, CA' => 1223400,
'Dallas, TX' => 1188580,
'San Antonio, TX' => 1144646,
'Detroit, MI' => 951270);
ksort(\$population);
\$total_population = 0;
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";
asort(\$population);
print "<table><tr><th>City</th><th>Population</th></tr>\n";
foreach (\$population as \$city => \$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";
?>
CityPopulation
Chicago, IL2896016
Dallas, TX1188580
Detroit, MI951270
Houston, TX1953631
Los Angeles, CA3694820
New York, NY8008278
Phoenix, AZ1321045
San Antonio, TX1144646
San Diego, CA1223400
Total23899236
CityPopulation
Detroit, MI951270
San Antonio, TX1144646
Dallas, TX1188580
San Diego, CA1223400
Phoenix, AZ1321045
Houston, TX1953631
Chicago, IL2896016
Los Angeles, CA3694820
New York, NY8008278
Total23899236

## PHP Exercise #8

### Data table built with an array

Modification of the previous exercise to display also the total population for each one of the states, arranging them by population in descending order.

<?php
// Separate city and state in the array so they can be added by state
\$population = array('New York' => array('state' => 'NY', 'pop' => 8000278),
'Los Angeles' => array('state' => 'CA', 'pop' => 3694820),
'Chicago' => array('state' => 'IL', 'pop' => 2896016),
'Houston' => array('state' => 'TX', 'pop' => 1953631),
'Philadelphia' => array('state' => 'PA', 'pop' => 1517550),
'Phoenix' => array('state' => 'AZ', 'pop' => 1321045),
'San Diego' => array('state' => 'CA', 'pop' => 1223400),
'Dallas' => array('state' => 'TX', 'pop' => 1188580),
'San Antonio' => array('state' => 'TX', 'pop' => 1144646),
'Detroit' => array('state' => 'MI', 'pop' => 951270));
// Utilizes the array \$state_totals to register the totals by state
\$state_totals = array();
\$total_population = 0;
print "<table><tr><th>City/State</th><th>Population</th></tr>\n";
foreach (\$population as \$city => \$info) {
// \$info is an array with two elements:
// pop (population of the city) and state (name of the state)
\$total_population += \$info['pop'];
// Increases the element \$info['state'] in \$state_totals
// by \$info['pop'] to register the total population
if (! array_key_exists(\$info['state'], \$state_totals)) {
\$state_totals[\$info['state']] = 0;
}
\$state_totals[\$info['state']] += \$info['pop'];
print "<tr><td>\$city, {\$info['state']}</td><td>{\$info['pop']}</td></tr>\n";
}
// Orders the array \$state_totals by population in descending order";
arsort (\$state_totals);
// Passes thru the array \$state_totals to print the totals by state
foreach (\$state_totals as \$state => \$pop) {
print "<tr><td>\$state</td><td>\$pop</td>\n";
}
print "<tr><td>Total</td><td>\$total_population</td></tr>\n";
print "</table>\n";
?>
City/StatePopulation
New York, NY8000278
Los Angeles, CA3694820
Chicago, IL2896016
Houston, TX1953631