daveoliveruk Posted July 14, 2008 Share Posted July 14, 2008 I have a load of the array entries below, what I need to know is how to loop through them making the 'internal name' the id for a select box and the 'options' the options for the select box. Any help would be greatly appreciated!! $data['profile:details'][] = (object)(array( "name" => __gettext("Are you circumsized? "), "internal_name" => "circumsized", "field_type" => "select", "options" => array( 'yes'=>__gettext('Yes'), 'no'=>__gettext('No'), 'na'=>__gettext('Not Applicable')), "description" =>"", "user_type" => "person", "category" => __gettext("Your Personal Information"), "col1" => true, "invisible" => false, "required" => false, )); $data['profile:details'][] = (object)(array( "name" => __gettext("What type of body hair do you have?"), "internal_name" => "body_hair", "field_type" => "select", "options" => array( 'yes'=>__gettext('Yes'), 'no'=>__gettext('No'), 'na'=>__gettext('Not Applicable')), "description" =>"", "user_type" => "person", "category" => __gettext("Your Personal Information"), "col1" => true, "invisible" => false, "required" => false, )); Quote Link to comment https://forums.phpfreaks.com/topic/114686-making-dropdown-boxes-from-array/ Share on other sites More sharing options...
.josh Posted July 14, 2008 Share Posted July 14, 2008 <?php // example array $something = array('a' => 1, 'b' => 2, 'c' => 3); echo "<form action = '' method = ''>"; echo "<select name = 'blah'>"; foreach($something as $key => $val) { echo "<option value = '$key'>$val</option>"; } // end foreach $something echo "</select>"; echo "</form>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/114686-making-dropdown-boxes-from-array/#findComment-589763 Share on other sites More sharing options...
daveoliveruk Posted July 14, 2008 Author Share Posted July 14, 2008 Thanks for the reply but that wouldn't work with the arrays that I have though, if you see above. Quote Link to comment https://forums.phpfreaks.com/topic/114686-making-dropdown-boxes-from-array/#findComment-589780 Share on other sites More sharing options...
.josh Posted July 14, 2008 Share Posted July 14, 2008 well that's just an example of looping through a simple array. You're obviously gonna have to apply it to your array. If you know how to echo out an individual target element then you can apply a foreach loop to that array, even if it's nested inside another array. Quote Link to comment https://forums.phpfreaks.com/topic/114686-making-dropdown-boxes-from-array/#findComment-589785 Share on other sites More sharing options...
wildteen88 Posted July 14, 2008 Share Posted July 14, 2008 PHP can loop through objects within an array too, so CV code still applies, example foreach($data['profile:details'] as $key => $obj) { echo '<p><b>' . $obj->name . "</b>\n"; echo '<select>'; foreach($obj->options as $key => $value) { echo "\n <option value=\"$key\">$value</option>"; } echo "\n</select><p>\n\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/114686-making-dropdown-boxes-from-array/#findComment-589826 Share on other sites More sharing options...
daveoliveruk Posted July 14, 2008 Author Share Posted July 14, 2008 Thanks wildteen thats a great help! Quote Link to comment https://forums.phpfreaks.com/topic/114686-making-dropdown-boxes-from-array/#findComment-589851 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.