[SOLVED] Mulitdemensional Arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

[SOLVED] Mulitdemensional Arrays

Post by Steveo31 »

Havin some problems with the following code:

Code: Select all

$categories = array(
              "Furniture" => array("Art", "Pictures", "Antiquities", "Item Managers", "Decoration", "Movable")
             , "Family" => array("Toys", "Motherhood", "Jewels and Perfumes", "Pharmacy")
             , "Tools/Equipment/Materials" => array("Tools", "Heavy Equipment", "Generators", "Pro Equipment", "Rentals", "Agricultural Tools")
             , "Animals" => array("Accessories", "Bedding")
             , "Real Estate" => array("Commerce", "Business", "Residential", "Industrial/Machine")
             , "Hardware" => array("Electrical", "Plumbing", "Electrical", "Sanitation")
             , "Education" => array("Books", "Computers", "Printers/Peripherals", "School/Office Supplies")
             , "Data Processing" => array("Software", "Games", "Computer Accessories")
             , "Electronics" => array("Audio", "Video", "Video Game Consoles", "Phones", "Cell Phone Accessories")
             , "Leisures" => array("Sports Equipment", "Photography", "Music", "Musical Instruments", "Movies", "Collections", "Hobbies")
             , "Community" => array("Sharing")
             , "Automobiles" => array("Directories", "Accessories", "Cars", "Equipment")
             , "Employment" => array("Job Fairs", "Seminars", "Frameworks")
         );
I'd like to get it where there's a select dropdown like so:

Please Choose
---
Furniture
Art
Pictures
etc...

I thought I had it, but the nested foreach loops were not was I was after.

A link would be much help as well :D
Last edited by Steveo31 on Wed Oct 06, 2004 2:22 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

what was the code you were trying.. we'll move from there.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Code: Select all

$categories = array(
              "Furniture" => array("Art", "Pictures", "Antiquities", "Item Managers", "Decoration", "Movable")
             , "Family" => array("Toys", "Motherhood", "Jewels and Perfumes", "Pharmacy")
             , "Tools/Equipment/Materials" => array("Tools", "Heavy Equipment", "Generators", "Pro Equipment", "Rentals", "Agricultural Tools")
             , "Animals" => array("Accessories", "Bedding")
             , "Real Estate" => array("Commerce", "Business", "Residential", "Industrial/Machine")
             , "Hardware" => array("Electrical", "Plumbing", "Electrical", "Sanitation")
             , "Education" => array("Books", "Computers", "Printers/Peripherals", "School/Office Supplies")
             , "Data Processing" => array("Software", "Games", "Computer Accessories")
             , "Electronics" => array("Audio", "Video", "Video Game Consoles", "Phones", "Cell Phone Accessories")
             , "Leisures" => array("Sports Equipment", "Photography", "Music", "Musical Instruments", "Movies", "Collections", "Hobbies")
             , "Community" => array("Sharing")
             , "Automobiles" => array("Directories", "Accessories", "Cars", "Equipment")
             , "Employment" => array("Job Fairs", "Seminars", "Frameworks")
         );

echo $categories['Leisures'][4];
// echos Movies
It is easier to put the commas at the end of the line. PHP allows a comma after the last element.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

What's the error you're getting?
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

My bad, no error. Kinda danced around this:

Code: Select all

$select = "<select name='category'>\n";
$select .= "<option value=''>Please Choose</option>\n";
$select .= "<option value=''>&nbsp;</option>\n";

foreach($categories as $sub){
    foreach($sub as $option){
        $select .= "<option value='$option'>&nbsp;&nbsp;&nbsp;&nbsp;$option</option>\n";
    }
}
    
$select .= "</select>\n";
Made some changes, almost had it at one point, but made some changes and can't get back to where I was.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

foreach($categories as $cat => $arr)
  $select .= '<option value="" style="font-weight:bold;border-top:1px black solid">' . $cat . '</option>' . "\n";
    foreach($cat as $option)
      $select .= '<option value="' . $cat . '|' . $option . '">' . $option . '</option>' . "\n";
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

Awesome. Thanks again.
Post Reply