[prev in list] [next in list] [prev in thread] [next in thread]
List: php-general
Subject: Re: [PHP] multi dimensional array question
From: Piero Steinger <piero () the-admins ! ch>
Date: 2010-04-30 23:23:20
Message-ID: 4BDB6668.3030901 () the-admins ! ch
[Download RAW message or body]
Am 01.05.2010 00:57, schrieb Nick Balestra:
> hello everybody here is my array(s)
>
>
> $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>";
>
>
> // $state is the key and $states is the value
> foreach ($us_census as $state => $cities) {
>
> // $state is the key and $habitant is the value
> foreach ($cities as $city => $habitants){
>
>
>
> print "<tr><td>$state</td><td>$city</td><td>$habitants</td><td></td></tr>";
>
>
> }
> }
>
>
> Now i also want to be able to count the total population per state, i am stucked...
>
array_sum() should do it :)
foreach ($us_census as $state => $cities) {
$population_per_state = array_sum($cities);
}
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic