Page 1 of 1

[SOLVED] Mulitdemensional Arrays

Posted: Tue Oct 05, 2004 3:31 pm
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

Posted: Tue Oct 05, 2004 3:38 pm
by feyd
what was the code you were trying.. we'll move from there.

Posted: Tue Oct 05, 2004 4:19 pm
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.

Posted: Tue Oct 05, 2004 4:21 pm
by patrikG
What's the error you're getting?

Posted: Tue Oct 05, 2004 4:40 pm
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.

Posted: Tue Oct 05, 2004 4:47 pm
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";

Posted: Wed Oct 06, 2004 2:20 am
by Steveo31
Awesome. Thanks again.