array += array
Posted: Fri Feb 13, 2009 5:32 pm
Hello,
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
heres the excersise
heres the line im not sure with:
so state_totals has been set as an empty array, but the way im seeing it is 'state' and 'pop' would both be made a value in state_totals, not a key and a value. can someone explain the use of the addition assignment operator with arrays
thanks in advance!
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!