内部foreach循环检查条件,并仅中断php中的内部循环

我有两个嵌套的foreach循环,我只希望内部循环检查条件。如果满足或不满足,则终止内循环并返回外循环。


在我的情况下,据我所知,我的内循环总是需要运行一次。


问题:两个数组。首先拥有所有选择。第二只选择。现在算出ID。如果匹配=>打印已选中,否则=>未选中。


我尝试过休息;但内部循环仅检查第一项,然后执行其他部分中的所有迭代。


@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

第一次检查并打印“已检查”,下次仅执行ifelse部分。我不知道为什么只检查第一。


所有其他都保持未选中状态。


慕田峪4524236
浏览 139回答 1
1回答

拉风的咖菲猫

您可以通过在视图中将选定的选项作为数组传递来简化代码。// Given $selectedOptions = [1, 2, 3, 4...]@foreach($property->amenities as $amenity)&nbsp; &nbsp; <label class="checkbox-inline control-label">&nbsp; &nbsp; &nbsp; &nbsp; <input type="checkbox"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;name="amenity[]"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;value="{{$amenity->id}}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@if (in_array($amenity->id, $selectedOptions))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;checked="checked"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@endif&nbsp; &nbsp; &nbsp; &nbsp; >{{ $amenity->name }}&nbsp; &nbsp; </label>&nbsp;&nbsp;@endforeach
打开App,查看更多内容
随时随地看视频慕课网APP