Page 1 of 1

array += array

Posted: Fri Feb 13, 2009 5:32 pm
by daveeeeee
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 :D heres the excersise

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";
?>
heres the line im not sure with:

Code: Select all

$state_totals[$info['state']] += $info['pop'];
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!

Re: array += array

Posted: Fri Feb 13, 2009 5:44 pm
by Mark Baker
I've used $firstArray += $secondArray as an alternative to $firstArray = array_merge($firstArray,$secondArray) in the past because it actually executed more quickly than array_merge, but found it inconsistent in behaviour between different versions of PHP; so now I always use array_merge() because that always behaves consistently (even if somewhat more slowly).

+ is a valid array operator, so logically += should be valid as well - in exactly the way it is for arithmetic operators - but the manual has a few notes on its slightly unusual behaviour

Re: array += array

Posted: Fri Feb 13, 2009 7:44 pm
by Chris Corbyn
Mark Baker wrote:I've used $firstArray += $secondArray as an alternative to $firstArray = array_merge($firstArray,$secondArray)
Actually it's slightly different. "+" is the union operator on arrays. A union won't overwrite any existing keys like array_merge() does. You'd need to reverse the $firstArray and $secondArray arguments for something a little closer. A union also handles associative arrays the same as non-associative arrays, whereas array_merge() handles them differently.


The code the OP posted is not creating a union between two arrays however. It should be just incrementing numbers "+=" is the operator to increment a number by the value on the right of it.

Re: array += array

Posted: Fri Feb 13, 2009 8:29 pm
by daveeeeee
i've just understood it :p having typed it out in a simpler manner!

$state_total['state'] = 'pop'; creates an array in the way i've come to know, but the += is there for when the same state comes up twice, and the population needs incrementing, so $state_totals[$info['state']] += $info['pop']; does two different things, creates an array or modifies an existing array!

im such a noob :p

p.s. thanks for the help!