使用活动记录在Yii2中创建radioList

时间:2018-08-04 08:46:32

标签: php yii2 yii2-advanced-app

$list = [];
foreach($choices as $choice)
{
    ArrayHelper::map($list, $choice->id, $choice->choice_text);            
}
echo $form->field($model, 'choice_text')->radioList($list)->label(false);

$choices从包含模型选择的所有对象的视图传递。我需要创建一个将$choice->id映射到$choice->choice_text的数组。我该怎么办?

2 个答案:

答案 0 :(得分:1)

$list = ArrayHelper::map($choices, 'id', 'choice_text');
$form->field($model, 'id')->radioList($list)->label(false);

有关更多示例,请参见https://www.yiiframework.com/doc/guide/2.0/en/helper-array#building-maps

答案 1 :(得分:0)

$list = [];
foreach($choices as $choice)
{
    $list = $list + [$choice->id=>$choice->choice_text];            
}
echo $form->field($model, 'choice_text')->radioList($list)->label(false);