如何在Yii的activeDropDownList中选择listData的索引值?

时间:2014-06-23 01:48:02

标签: php yii

我在listData中有一个数组,我想通过activeDropDownList获取值,而不是索引,这是我的代码:

$salaries=array('1000$', '2000$', '3000$', '4000$', '5000$');
echo CHtml::activeDropDownList($model,'salary', $salaries,
                               array('empty'=>'Select salary',
                               'style'=>'width:300px', 'class'=>'form-control'));

当数据发送到控制器的值为$ model-> salary = 1,如果用户选择2000 $,我希望该值为2000 $。

1 个答案:

答案 0 :(得分:2)

试试这个array_combine

$salaries=array_combine($salaries,$salaries);

你得到数组array('1000$'=>'1000$','2000$'=> '2000$','3000$'=> '3000$', '4000$'=> '4000$', '5000$'=> '5000$')

这是你应该的,

$salaries=array('1000$', '2000$', '3000$', '4000$', '5000$');
$salaries=array_combine($salaries,$salaries);
echo CHtml::activeDropDownList($model,'salary', $salaries,
                               array('empty'=>'Select salary',
                               'style'=>'width:300px', 'class'=>'form-control'));