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

问题描述 投票:-1回答:2
$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的数组。我该怎么办呢?

php yii2 yii2-advanced-app
2个回答
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


0
投票
$list = [];
foreach($choices as $choice)
{
    $list = $list + [$choice->id=>$choice->choice_text];            
}
echo $form->field($model, 'choice_text')->radioList($list)->label(false);
© www.soinside.com 2019 - 2024. All rights reserved.