这是你想做的吗?
function renderSelector($attr=array(),$options=array(),$selected=''){
if(@count($attr))
foreach($attr as $k=>$v) $attr[$k] = $k.'="'.htmlentities($v).'"';
if(@count($options))
foreach($options as $k=>$v) $options[$k] = '<option value="'.htmlentities($k).'" '.(($k==$selected)?'selected':'').'>'.$v.'</option>';
return $str = '<select '.implode(' ',$attr).'>'.implode('',$options).'</select>';
}
echo renderSelector(
$attr=array('name'=>'city'),
$options=array('NY'=>'Newyork','TK'=>'Tokyo','3'=>'London'),
$selected='TK'
);
演示