$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
的数组。我该怎么办?
答案 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);