The following function (similar to one above) will render an array as a series of HTML select options (i.e. "<option>...</option>"). The problem with the one before is that there was no way to handle <optgroup>, so this function solves that issue.
function arrayToSelect($option, $selected = '', $optgroup = NULL)
{
$returnStatement = '';
if ($selected == '') {
$returnStatement .= '<option value="" selected="selected">Select one...</option>';
}
if (isset($optgroup)) {
foreach ($optgroup as $optgroupKey => $optgroupValue) {
$returnStatement .= '<optgroup label="' . $optgroupValue . '">';
foreach ($option[$optgroupKey] as $optionKey => $optionValue) {
if ($optionKey == $selected) {
$returnStatement .= '<option selected="selected" value="' . $optionKey . '">' . $optionValue . '</option>';
} else {
$returnStatement .= '<option value="' . $optionKey . '">' . $optionValue . '</option>';
}
}
$returnStatement .= '</optgroup>';
}
} else {
foreach ($option as $key => $value) {
if ($key == $selected) {
$returnStatement .= '<option selected="selected" value="' . $key . '">' . $value . '</option>';
} else {
$returnStatement .= '<option value="' . $key . '">' . $value . '</option>';
}
}
}
return $returnStatement;
}
So, for example, I needed to render a list of states/provinces for various countries in a select field, and I wanted to use each country name as an <optgroup> label. So, with this function, if only a single array is passed to the function (i.e. "arrayToSelect($stateList)") then it will simply spit out a bunch of "<option>" elements. On the other hand, if two arrays are passed to it, the second array becomes a "key" for translating the first array.
Here's a further example:
$countryList = array(
'CA' => 'Canada',
'US' => 'United States');
$stateList['CA'] = array(
'AB' => 'Alberta',
'BC' => 'British Columbia',
'AB' => 'Alberta',
'BC' => 'British Columbia',
'MB' => 'Manitoba',
'NB' => 'New Brunswick',
'NL' => 'Newfoundland/Labrador',
'NS' => 'Nova Scotia',
'NT' => 'Northwest Territories',
'NU' => 'Nunavut',
'ON' => 'Ontario',
'PE' => 'Prince Edward Island',
'QC' => 'Quebec',
'SK' => 'Saskatchewan',
'YT' => 'Yukon');
$stateList['US'] = array(
'AL' => 'Alabama',
'AK' => 'Alaska',
'AZ' => 'Arizona',
'AR' => 'Arkansas',
'CA' => 'California',
'CO' => 'Colorado',
'CT' => 'Connecticut',
'DE' => 'Delaware',
'DC' => 'District of Columbia',
'FL' => 'Florida',
'GA' => 'Georgia',
'HI' => 'Hawaii',
'ID' => 'Idaho',
'IL' => 'Illinois',
'IN' => 'Indiana',
'IA' => 'Iowa',
'KS' => 'Kansas',
'KY' => 'Kentucky',
'LA' => 'Louisiana',
'ME' => 'Maine',
'MD' => 'Maryland',
'MA' => 'Massachusetts',
'MI' => 'Michigan',
'MN' => 'Minnesota',
'MS' => 'Mississippi',
'MO' => 'Missouri',
'MT' => 'Montana',
'NE' => 'Nebraska',
'NV' => 'Nevada',
'NH' => 'New Hampshire',
'NJ' => 'New Jersey',
'NM' => 'New Mexico',
'NY' => 'New York',
'NC' => 'North Carolina',
'ND' => 'North Dakota',
'OH' => 'Ohio',
'OK' => 'Oklahoma',
'OR' => 'Oregon',
'PA' => 'Pennsylvania',
'RI' => 'Rhode Island',
'SC' => 'South Carolina',
'SD' => 'South Dakota',
'TN' => 'Tennessee',
'TX' => 'Texas',
'UT' => 'Utah',
'VT' => 'Vermont',
'VA' => 'Virginia',
'WA' => 'Washington',
'WV' => 'West Virginia',
'WI' => 'Wisconsin',
'WY' => 'Wyoming');
...
<select name="state" id="state"><?php echo arrayToSelect($stateList,'',$countryList) ?></select>
<select name="country" id="country"><?php echo arrayToSelect($countryList,'US') ?></select>