Ik was net aardig op weg , toen...
ik ben op de errata sectie van de publisher site geweest, maar daar werd geen
melding van dit probleem gemaakt.
Zou iemand mij kunnen helpen ?
<?php
$population = array("New York" => array("state" => "ny", "pop" => 8008278),
"Los Angeles" => array("state" => "ca", "pop" => 3694820),
"Chicago" => array("state" => "IL", "pop" => 2896016),
"Houston" => array("state" => "TX", "pop" => 1953631),
"Philadlephia" => 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)
);
//use the state totals array to keep track of the states totals
$total_population = 0;
$state_totals = array();
echo "<table><tr><th>City</th><th>Population</th></tr>\n";
foreach($population as $city => $info) {
//$info is an array with two elements: pop (city population)
//and state (state name)
$total_population += $info["pop"];
//increment the $info["state"] element in $state_totals by $info["pop"]
//to keep track of the total population of state $info["state"]
$state_totals[$info["state"]] += $info["pop"];
echo "<tr><td>$city, {$info['state']}</td><td>{$info['pop']}</td></tr>\n";
}
//iterate through the $state_totals array to echo the per state totals
foreach ($state_totals as $state => $pop) {
echo "<tr><td>$state</td><td>$pop</td>\n";
}
echo "<tr><td>Total</td><td>$total_population</td></tr>\n";
echo "</table>\n";
?>
2.176 views