[英]Foreach is not working for two arrays
I have an array 我有一个数组
$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
)
)
and 和
$key1=Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
)
Here $key1 means index of $assid 这里$ key1表示$ assid的索引
And a foreach loop 还有一个foreach循环
@foreach($roles as $id=>$name)
<option value="{{$id}}"@if(in_array($id, $assid))selected="selected"@endif>{{$name}}</option>
@endforeach
Here roles have 12 values.so array will iterate 12 times.my problem is that I want to append $assid[0],[1],[2],[3] so i $key1 values with foreach so I tried 这里的角色有12个值。因此数组将迭代12次。我的问题是我想附加$ assid [0],[1],[2],[3],所以我将$ key1值与foreach一起使用,所以我尝试了
$a=0;
@foreach($roles as $id=>$name)
<option value="{{$id}}"@if(in_array($id, $assid[$a]))selected="selected"@endif>{{$name}}</option>
<?php $a++;?>
@endforeach
Because $key1 array as only index 0 1 2 3.so i need $a should be incremented as 0 to 3 inside foreach loop 12 iterations are there so after 3 it shows undefined index 4.i didn't get a proper solution. 因为$ key1数组仅作为索引0 1 2 3.so,所以我需要$ a应该在foreach循环内从0递增到3,因此存在12次迭代,因此在3之后显示未定义的索引4.i没有得到适当的解决方案。 please help me.Please
请帮助我。
$a=0;
@foreach($roles as $id=>$name)
<option value="{{$id}}"@if(in_array($id, $assid[$a]))selected="selected"@endif>{{$name}}</option>
<?php $a++;?
if(a>4){
a = 0;
}>
@endforeach
it make value zero if you are more then 4 so it start agin from zero 如果您大于4,则将值设为零,因此从零开始
If i understand your problem you are looking for this stuff...Try this :- 如果我了解您的问题,您正在寻找这些东西...请尝试:-
$assid=Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
)
$roles=Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] => 6
[6] => 7
[8] => 8
[9] => 9
[10] => 10
[11] => 11
)
@foreach($roles as $id => $name)
@if(isset($assid[$id]))
<option value="{{$id}}"@if(in_array($id, $assid))selected="selected"@endif>{{$name}}</option>
@else
<option value="{{$id}}">{{$name}}</option>
@endif
@endforeach
Hope it helps! 希望能帮助到你!
Possibly it works as expected. 可能按预期工作。
$a=0;
@foreach($roles as $id=>$name)
<?php if($a>3) $a=0; ?>
<option value="{{$id}}"@if(in_array($id, $assid[$a]))selected="selected"@endif>{{$name}}</option>
<?php $a++; ?>
@endforeach
You need to round-robin back from N
to 0
. 您需要从
N
循环回到0
。 Using an index that is modulo N + 1 of the counter guarantees this. 使用计数器的模N +1的索引可以保证这一点。
To round-robin through the first 3 items of an array, where N=2 because arrays have zero-based indexing, we'll take modulo 3 为了遍历数组的前3个项(其中N = 2,因为数组具有从零开始的索引),我们将取模3
@php ($a = 0)
@foreach($roles as $id=>$name)
<option
value="{{$id}}"
@if(in_array($id, $assid[$a % 3]))
selected="selected"
@endif
>{{ $name }}</option>
@php ($a = $a + 1)
@endforeach
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.