有没有办法在下拉框in_array中设置所选项目。 但是Selected =“selected”对我不起作用。只有少数选择的值显示下拉并且没有被选中
我尝试了以下代码
List
这里:
<label for="assignedto">Assigned to</label>
@foreach($menu as $menus)
<?php
$assignedid=$menus->assigned_to;
$assid[]=explode(',',$assignedid);
?>
@endforeach
<?php $maxcount= count($assid); echo '<pre>';print_r($assid); ?>
<select id="framework1" name="Roles[]" multiple class="form-control" >
<?php $a= 0;?>
@foreach($roles as $id=>$name)
<?php $newcount=count($assid[$a]);?>
<option value="{{$id}}"@if(in_array($id, $assid[$a]))selected="selected"@endif>{{$name}}</option>
<?php
$a++;
if($a==$maxcount) break;
?>
@endforeach
角色数组是:
$assid=Array
(
[0] => Array
(
[0] => 6
[1] => 2
[2] => 3
)
[1] => Array
(
[0] => 4
[1] => 5
[2] => 6
)
[2] => Array
(
[0] => 6
)
[3] => Array
(
[0] => 2
[1] => 3
)
)
对于第一次迭代,我想选择id 4,5,6意味着经理,主管,测试人员应该被选中,同样第二次迭代4,5,6选择等 但选择多个选择对我不起作用。请帮助我。非常感谢
答案 0 :(得分:0)
我希望您需要重新编写下面的链接:
<select id="framework1" name="Roles[]" multiple class="form-control" >
@foreach($roles as $id=>$name)
<option value="{{$id}}"
@if(in_array($id, $assid[$a]))
selected="selected"
@endif
>{{$name}}</option>
@endforeach
</select>
答案 1 :(得分:0)
使用可以使用三元运算符代替&#34;如果&#34; -
<option value="{{$id}}" <?php echo ((in_array($id, $assid[$a])) ? 'selected="selected"':'')?> >{{$name}}</option>
答案 2 :(得分:0)
重写您的代码如下: -
<label for="assignedto">Assigned to</label>
<?php $assid = []; // declare array ?>
@foreach($menu as $menus)
<?php
if(!empty($menus->assigned_to)){ // check if assigned_to key exist in array
$assignedid=$menus->assigned_to;
$assid[]=explode(',',$assignedid);
}
?>
@endforeach
<?php
if(!empty($assid)){
$assid = call_user_func_array('array_merge', $assid); // It will shift all array elements up one level
}
$assid = empty($assid) ? [] : array_combine($assid, $assid); // rewrite this line
?>
<select id="framework1" name="Roles[]" multiple class="form-control" >
@foreach($roles as $id=>$name)
<option value="{{$id}}"@if(in_array($id, $assid))selected="selected"@endif>{{$name}}</option>
@endforeach
</select>