I've recently started learning php using "Learning PHP 5" an o'reily book, im reading it and working through the excersises. I've come across something i cant get my head around and i'm hoping someone can explain
Code: Select all
<?php
$census = array('New York' => array('state' => 'NY', 'pop' => '8008278'),
'Los Angeles' => array('state' => 'LA', 'pop' => '3694820'),
'Chicago' => array('state' => 'IL', 'pop' => '2896016'),
'Houston' => array('state' => 'TX', 'pop' => '1953631'),
'Philadelphia' => array('state' => 'PA', 'pop' => '1517550'),
'Pheonix' => 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'));
$state_totals = array();
$total_population = 0;
print '<table border="1"><tr><th>City</th><th>Population</th></tr>';
foreach ($census as $city => $info) {
$total_population += $info['pop'];
$state_totals[$info['state']] += $info['pop'];
print "<tr><td>$city, {$info['state']}</td><td>{$info['pop']}</td></tr>";
}
foreach ($state_totals as $state => $pop) {
print "<tr><td>$state</td><td>$pop</td></tr>";
}
print "</table>";
print "the total population is $total_population";
?>Code: Select all
$state_totals[$info['state']] += $info['pop'];thanks in advance!