Omzy Posted May 20, 2010 Share Posted May 20, 2010 Here is my Categories array: $cats=array( 'decorations'=>array('Decorations', 'Asian Wedding Decorations'), 'flowers'=>array('Flowers', 'Asian Wedding Flowers'), ); Currently I print this out to to a form on the page as follows: foreach($cats as $index => $value) { echo ' <input type="checkbox" name="selection[]" value="'.$index.'" id="'.$index.'" /> <label for="'.$index.'">'.$value[0].'</label> '; } So this is all fine and dandy. Now I also have a Subcategories array, which uses the first element of its array to join with the Categories array: $subcats=array( 'balloons'=>array('decorations', 'Balloons'), 'banners'=>array('decorations', 'Banners'), 'roses'=>array('flowers', 'Roses'), 'tulips'=>array('flowers', 'Tulips'), ); So basically I would like to print out the subcategories of the parent category in square brackets, for example: Decorations [balloons, Banners] Flowers [Roses, Tulips] Link to comment https://forums.phpfreaks.com/topic/202420-slightly-complex-multidimensional-array-issue/ Share on other sites More sharing options...
Psycho Posted May 20, 2010 Share Posted May 20, 2010 That makes no sense. It would take a lot more processing to do it with that subcat array. Can you change the format of your $subcat array to this: $subcats=array( 'decorations'=>array('Banners', 'Balloons'), 'flowers'=>array('Tulips', 'Roses') ); Or if you need the lowercase keys: $subcats=array( 'decorations'=>array('banners'=>'Banners', 'balloons'=>'Balloons'), 'flowers'=>array('yulips'=>'Tulips', 'roses'=>'Roses') ); Link to comment https://forums.phpfreaks.com/topic/202420-slightly-complex-multidimensional-array-issue/#findComment-1061294 Share on other sites More sharing options...
Omzy Posted May 20, 2010 Author Share Posted May 20, 2010 Thanks mjdamato. So now how do I go about outputting the data to my requirements? Link to comment https://forums.phpfreaks.com/topic/202420-slightly-complex-multidimensional-array-issue/#findComment-1061309 Share on other sites More sharing options...
Psycho Posted May 20, 2010 Share Posted May 20, 2010 foreach($cats as $cat_index => $cat_value) { //Display category echo " <input type=\"checkbox\" name=\"selection[]\" value=\"{$cat_index}\" id=\"{$cat_index}\" />"; echo "<label for=\"{$cat_index}\">{$cat_value[0]}</label><br />"; //Display subcats for the current category foreach($subcats[$cat_index] as $subcat_value) { echo " - {$subcat_value}<br />\n"; } } Link to comment https://forums.phpfreaks.com/topic/202420-slightly-complex-multidimensional-array-issue/#findComment-1061372 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.