對於Zend_Form_Element_Select()它是這樣的
$multiOptions = array(
'Group A' => array(1 => 'First Value',2 => 'Second Value A),
'Group B' => array(3 => 'Third Value'),
);
$element->setMultiOptions($multiOptions);
請注意,你也有addMultiOption($選項,$ value)和addMultiOptions($選項)。只需在一個額外的數組中包含值或選項。
$this->add(array(
'name'=>'Test',
'type'=>'Zend\Form\Element\Select',
'attributes'=>array('type'=>'select','required'=>'required'),
'options'=>array(
'label'=>'Test',
'value_options'=>array('fruits'=>array('label'=>'Fruits','options'=>array('1'=>'Apple','2'=>'Mango')),'animals'=>array('label'=>'Animals','options'=>array('cat'=>'CAT','dog'=>'DOG'))),
'empty_option'=>'Please Select'
),
));
請注意,empty_options選項指定不存在代替empty_option應使用:
我怎麼能'A組'也是ID?像001 – YumYumYum 2012-02-23 07:47:42
#Google不確定你的ID是什麼意思。 「optgroup」不是您可以選擇的項目。在Zend中,數組「A」的關鍵字變成了標籤,標籤在下拉菜單中看起來像一個標題。請參閱[W3Schools](http://www.w3schools.com/html5/tryit.asp?filename=tryhtml5_optgroup) – 2012-02-23 13:55:45
我對使用XML進行回答的問題感興趣,因爲它已被請求,這對Zend_Config_XML和Zend_Form_Element很有用 – axiom82 2014-06-24 16:11:31