Retain selected option when posting errors in form

The user’s choice of select option is not currently retained when an error occurs in my contact form, which I know will often be overlooked when it goes live. I found the following solution in O’Reilly’s PHP Cookbook (part of example 9-22 on p278), but it does not create value attributes for each option. I would much prefer to use a method that includes value attributes because my values are shorter than the text that is displayed to the user (they are used as the title of the e-mail).

$flavors = array('Vanilla','Chocolate','etc.');

foreach ($flavors as $flavor) {
if (isset($_POST['flavor']) && ($_POST['flavor'] == $flavor)) {
$defaults['flavor'][$flavor] = "selected='selected'";
} else {
$defaults['flavor'][$flavor] = ''; }

foreach ($flavors as $flavor) {
echo "<option {$defaults['flavor'][$flavor]}>$flavor</option>";}

So, is it possible to build the same kind of function, but with value attributes included?

function html_options($arrOptions) {
	* Return string 
	$strReturn = '';
	* Extract values, output and selected
	$arrValues = isset($arrOptions['values'])?$arrOptions['values']:null;
	$arrOutput = isset($arrOptions['output'])?$arrOptions['output']:null;
	$strSelected = isset($arrOptions['selected'])?$arrOptions['selected']:null;
	* Values and output are required to build option list 
	if($arrValues === null || $arrOutput === null) return $strReturn;
	$intValues = count($arrValues);
	for($i=0;$i<$intValues;$i++) {
			'<option value="&#37;s"%s>%s</option>'
			,$strSelected == $arrValues[$i]?' selected="selected"':''
	return $strReturn;	
<?php echo html_options(array('values'=>array('van','choc','etc'),'output'=>array('Vanilla','Chocolate','etc.'),'selected'=>'choc')); ?>

Thanks very much. That’s very helpful.