Jump to content

[SOLVED] Print array variable (multidimensional)


unxposed

Recommended Posts

Okay I think this should be an easy one!

 

If I'm creating an array from a database like so:

 

while ($row = mysql_fetch_array($result)) {

$site[$row['group']][$row['title']][name] = $row['name'];

$site[$row['group']][$row['title']][colour] = $row['multiple_choice'];

$site[$row['group']][$row['title']][shape] = $row['setting'];

}

 

How would I then print the value of the group variable in the array $site[$row['group']] or title variable $site[$row['group']][$row['title']].

 

Thanks in advance!

Sorry I don't think I was clear.

 

For example if this was added to the array:

 

$site[first_group][first_item][name] = item_name;

 

how would I echo 'first_group', as this will vary (could be first_group, second_group, some_group, any_group etc.).

 

Thanks!

 

ok...still not 100% clear...do you mean this:

 

<?php
  foreach($site as $group=>$titles){
    print "Group: $group<br>";
    foreach($titles as $title=>$data){
      print "--Title: $title<br>";
      print "----Name: {$data['name']}<br>";
      print "----Colour: {$data['colour']}<br>";
      print "----Shape: {$data['shape']}<br>";
    }
  }
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.