Looping through Multidimensional Arrays

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hi all,

I'm taking a PHP class and one of our assignments is to create a page
where a user selects a province from a dropdown list and after
clicking the submit button the next page will display information
about that province taking it from the $provinces_data array.

I've managed to create the first page and can send the chosen province
to the second page but am stuck in how to figure out how to show the
data just for the selected province.

Here is pseudo-code for what I'm trying to do:

$province = "province chosen by user";

where $provinces_data == $province
show all values for $province;

Any help is appreciated. The $provinces_data array is below.


$provinces_data = array
('BC' => array('capital' => 'Victoria', 'premier'=> 'Gordon
'AB' => array('capital' => 'Edmonton', 'premier'=> 'Ed Stelmach'),
'SK' => array('capital' => 'Regina', 'premier'=> 'Brad Wall'),
'MB' => array('capital' => 'Winnipeg', 'premier'=> 'Gary Doer'),
'ON' => array('capital' => 'Toronto', 'premier'=> 'Dalton McGuinty'),
'QC' => array('capital' => 'Quebec City', 'premier'=> 'Jean Charest'),
'NL' => array('capital' => 'St. John\'s', 'premier'=> 'Danny
'NB' => array('capital' => 'Fredericton', 'premier'=> 'Shawn Graham'),
'NS' => array('capital' => 'Halifax', 'premier'=> 'Rodney
'PE' => array('capital' => 'Charlottetown', 'premier'=> 'Robert

Re: Looping through Multidimensional Arrays

FCB schreef:
Quoted text here. Click to load it

Presuming that the value of the incoming chosen province matches the
array keys (the abbreviations, for instance 'BC' ):

$data = $provinces_data[ $province ];
print_r( $data );

Then, if you wanted to access the capital:

$capital = $data[ 'capital' ];
print_r( $capital );

If you would like to access the capital directly:

$capital = $provinces_data[ $province ][ 'capital' ];
print_r( $capital );

Same principle for premier:

$premier = $provinces_data[ $province ][ 'premier' ];
print_r( $premier );

Re: Looping through Multidimensional Arrays

On Fri, 13 Feb 2009 05:22:14 -0800 (PST),

Quoted text here. Click to load it

foreach ( $provinces_data as $prov_code => $prov )
    echo 'The premier of '.$prov_code.' is '.$prov['premier'].'
        and the capital is '.$prov['capital'].'<br>' ;

Re: Looping through Multidimensional Arrays

A.Reader wrote:
Quoted text here. Click to load it

A.Reader - why would you spoon feed the answer to him?  If he has stated
  it is "homework", how is he going to learn if you give him the answer.
  Give him "help" but don't give him the answer - it only creates more
"programmers" that will some day have yours and my job at half our rates
and post their "work" here and you would gladly give it to him for free.
   There is MORE than enough docs for you to refer him.  If his
"professor" is smart - he will check this NG and revoke his grade for
this exercise.

Site Timeline