内部foreach循环检查条件,并在php中仅中断内部循环
我有两个foreach循环嵌套,我只希望内部循环检查条件。如果它满足或不满足其中任何一个,则终止内部循环并返回外部循环 在我的情况下,当我理解我的问题时,内部循环总是需要运行一次 问题:两个数组。首先要有所有的选择。第二,只有选择。现在计算id。如果匹配=>Print选中,否则=>UnChecked 我试过突破;但内部循环只检查第一项,然后执行其他部分中的所有迭代内部foreach循环检查条件,并在php中仅中断内部循环,php,laravel,Php,Laravel,我有两个foreach循环嵌套,我只希望内部循环检查条件。如果它满足或不满足其中任何一个,则终止内部循环并返回外部循环 在我的情况下,当我理解我的问题时,内部循环总是需要运行一次 问题:两个数组。首先要有所有的选择。第二,只有选择。现在计算id。如果匹配=>Print选中,否则=>UnChecked 我试过突破;但内部循环只检查第一项,然后执行其他部分中的所有迭代 @php foreach($propertyAmenities as $amenity){ foreach($property
@php
foreach($propertyAmenities as $amenity){
foreach($property->amenities as $new){
if( ($amenity->type == 'amenity') && ($amenity->id == $new->id) ){
@endphp
<label class="checkbox-inline control-label">
<input type="checkbox" name="amenity[]" value="{{$amenity->id}}" {{'checked'}}>{{ $amenity->name }}
</label>
@php break;
}
elseif(($amenity->type == 'amenity')){ @endphp
<label class="checkbox-inline control-label">
<input type="checkbox" name="amenity[]" value="{{$amenity->id}}">{{ $amenity->name }}
</label>
@php break;
}
}
}
@endphp
@php
foreach($PROPERTYYAMENITIES as$MENCITY){
foreach($property->设施为$new){
如果($amenity->type=='amenity')&&($amenity->id==$new->id)){
@endphp
{{$amenity->name}
@php中断;
}
elseif(($amentity->type=='amentity')){@endphp
{{$amenity->name}
@php中断;
}
}
}
@endphp
它第一次检查并在下一次仅执行ifelse部分时打印“已检查”。我不知道为什么
只检查第一个
所有其他选项都保持未选中状态。您可以通过在视图中将所选选项作为数组传递来简化代码
// Given $selectedOptions = [1, 2, 3, 4...]
@foreach($property->amenities as $amenity)
<label class="checkbox-inline control-label">
<input type="checkbox"
name="amenity[]"
value="{{$amenity->id}}"
@if (in_array($amenity->id, $selectedOptions))
checked="checked"
@endif
>{{ $amenity->name }}
</label>
@endforeach
//给定$selectedOptions=[1,2,3,4…]
@foreach($property->便利设施作为$commency)
id,$selectedOptions))
checked=“checked”
@恩迪夫
>{{$amenity->name}
@endforeach
在$property->professionals
中便利设施
是一种关系方法吗?是的,这是一种关系谢谢你爱你