I updated the options_for_select function so that it will detect if an array value ($value) is an array. If so, it creates an <optgroup> with label = $key.
The code is below.
function options_for_select($options = array(), $selected = '', $html_options = array())
{
$html_options = _parse_attributes($html_options);
if (is_array($selected))
{
$valid = array_values($selected);
$valid = array_map('strval', $valid);
}
$html = '';
if (isset($html_options['include_custom']))
{
$html .= content_tag('option', $html_options['include_custom'], array('value' => ''))."\n";
}
else if (isset($html_options['include_blank']))
{
$html .= content_tag('option', '', array('value' => ''))."\n";
}
foreach ($options as $key => $value)
{
if (is_array($value))
{
$html .= content_tag('optgroup', options_for_select($value, $selected, $html_options), array('label' => $key))."\n";
}
else
{
$option_options = array('value' => $key);
if (
isset($selected)
&&
(is_array($selected) && in_array(strval($key), $valid, true))
||
(strval($key) == strval($selected))
)
{
$option_options['selected'] = 'selected';
}
$html .= content_tag('option', $value, $option_options)."\n";
}
}
return $html;
}