thanks Piero!

i was trying to solve an excercise on "learning php5" (O'reilyl) book.

I am happy abotut his solution with the array_sum funtion you suggested, and my 
multidimensional array make much more sense to mee then they suggested solution 
that also much more line of code comapred... 

look: my solution (with Piero suggeston): and ont he bottom the book solution. 
what do u say is the best one? why? i am learning so i am interested in 
understanding why a solution can be better then an other...

$us_census = array('NY' => array('New York' => 8008278),
                                   'CA' => array('Los Angeles' => 3694820,
                                                                 'San Diego' => 
1223400),
                                   'IL' => array('Chicago' => 2896016),
                                   'TX' => array('Houston' => 1953631,
                                                                 'Dallas' => 
1188580,
                                                                 'San Antonio' 
=> 1144646),
                                   'PA' => array('Philadelphia' => 1517550),
                                   'AZ' => array('Phoenix' => 1321045),
                                   'MI' => array('Detroit' => 951270)); 
                                   


print 
"<table><tr><th>State</th><th>City</th><th>Population</th><th>Total</th></tr>";


foreach ($us_census as $state => $cities) {

        foreach ($cities as $city => $habitants){

                $tothabitants += $habitants;
                
                print 
"<tr><td>$state</td><td>$city</td><td>$habitants</td><td></td></tr>";
                }
        }
        
print "<tr><td></td><td></td><td></td><td>$tothabitants</td></tr></table>";


foreach ($us_census as $state => $cities) {
    $population_per_state = array_sum($cities);
    print "$state $population_per_state<br>";
}

--------------------------
the book solution:


$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),
'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));

$state_totals = array( );
$total_population = 0;
print "<table><tr><th>City</th><th>Population</th></tr>\n";
foreach ($population 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>\n";
}

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";




Reply via email to