yii循环系列名称的数组

时间:2014-10-09 03:39:55

标签: php arrays loops yii indexing

我有如下数组:

radiolist = array (k1=>'1',k2=>'3',k3=>'2',k4=>'1',k5=>'2',k6=>'2');

在我的表单中,我想根据以下代码重复这些数组索引和值:

for ($i = 0; $i <= count($radiolist)-1; $i++) {  
  echo $form->radioButtonList($model,'k[$i]',array('1'=>'1','2'=>'2','3'=>'3'));
  //'k[$i]' repeated to be k1, k2, k3, k4, k5, k6   
}

我想要这个输出:

<input type="radio" name="school[k1]" value="1" /> 1
<input type="radio" name="school[k1]" value="2" /> 2
<input type="radio" name="school[k1]" value="3" /> 3
.
.
.
<input type="radio" name="school[k6]" value="1" /> 1
<input type="radio" name="school[k6]" value="2" /> 2
<input type="radio" name="school[k6]" value="3" /> 3
我真的很困惑怎么做。请任何建议表示高度赞赏。

感谢。

2 个答案:

答案 0 :(得分:0)

如果你只是想重复那些数组索引和值,radioButtonList()就会自己为你做。

<?php $radiolist = array ('k1'=>'1','k2'=>'3','k3'=>'2','k4'=>'1','k5'=>'2','k6'=>'2');  ?>
<?php echo $form->radioButtonList($model,'school',$radiolist,
   array(
     'template'=>'{input}{label}',
     'separator'=>'',
     'labelOptions'=>array(
            'style'=> '
                padding-left:13px;
                width: 60px;
                float: left;
            '),

      ));?>
?>

答案 1 :(得分:0)

尝试几种组合后,这是我的解决方案:

$k = array ('k1','k2','k3','k4','k5','k6');

for ($i = 0; $i <= count($k)-1; $i++) {  
   echo $form->radioButtonList($model,$k[$i], array('1'=>'1','2'=>'2','3'=>'3'));
   //$k[$i] would repeated to be k1, k2, k3, k4, k5, k6   
   //$i as index or use other index as u want.
}
非常感谢所有的建议。 欢呼声。