laravel Foreach嵌套

时间:2019-04-22 10:41:15

标签: php laravel

我有以下数组:

刀片代码:

@foreach($allediensten as $alledienstens)
    @foreach($beschikbaarheid as $beschikbaar)
        @if($beschikbaar->job_id !== $alledienstens->id)
            <tr>
                <td><input type="checkbox" class="checkbox" value="{{$alledienstens->id}}" name="checkbox[]" /></td>
                <td id="takenid">{{$alledienstens->id}}</td>
                <td>{{ \Carbon\Carbon::parse($alledienstens->date)->format('d-m-Y')}}</td>
                <td>{{ \Carbon\Carbon::parse($alledienstens->starttijd)->format('H:i')}}</td>
                <td>{{ \Carbon\Carbon::parse($alledienstens->eindtijd)->format('H:i')}}</td>
                <td>{{$alledienstens->kamernummer}}</td>
                <td>{{$alledienstens->aantalsurveillanten}}</td>
                <td>{{$alledienstens->locatie}}</td>
                <td>{{ \Carbon\Carbon::parse($alledienstens->beschikbaartot)->format('d-m-Y H:i')}}</td>
                <td>{{ \Carbon\Carbon::parse($alledienstens->accepttot)->format('d-m-Y H:i')}}</td>
            </tr>
        @endif
    @endforeach
@endforeach

数组:

1: [{"job_id":1},{"job_id":2}]

2: {"id":1,"user_id":3,"vaardigheid":"TDV","taal":"Nederlands","digitaal":"Ja","ervaring":"Meer dan 2 jaar","date":"2019-04-05","type":"Decentraal","starttijd":"19:15:00","eindtijd":"20:15:00","kamernummer":"H1.03","locatie":"test","beschikbaartot":"2019-04-11 00:00:00","accepttot":"2019-04-11 16:43:00","status":"uitgezonden","cronstatus":"beschikbaaropgeven","opmerking":null,"created_at":"2019-04-21 14:39:54","updated_at":"2019-04-21 22:26:28"},
{"id":2,"user_id":3,"vaardigheid":"TDV","taal":"Nederlands","digitaal":"Ja","ervaring":"Meer dan 2 jaar","date":"2019-04-05","type":"Decentraal","starttijd":"19:15:00","eindtijd":"20:15:00","kamernummer":"H1.03","locatie":"test","beschikbaartot":"2019-04-11 00:00:00","accepttot":"2019-04-11 16:43:00","status":"uitgezonden","cronstatus":"beschikbaaropgeven","opmerking":null,"created_at":"2019-04-21 14:39:54","updated_at":"2019-04-21 22:26:28"},
{"id":3,"user_id":3,"vaardigheid":"TDV","taal":"Nederlands","digitaal":"Ja","ervaring":"Meer dan 2 jaar","date":"2019-04-05","type":"Decentraal","starttijd":"19:15:00","eindtijd":"20:15:00","kamernummer":"H1.03","locatie":"test","beschikbaartot":"2019-04-11 00:00:00","accepttot":"2019-04-11 16:43:00","status":"uitgezonden","cronstatus":"beschikbaaropgeven","opmerking":null,"created_at":"2019-04-21 14:39:54","updated_at":"2019-04-21 22:26:28"}]

结果:这将显示结果ID 1、2、2、3、3。

如果查看数组,则必须给出结果:仅id 3。

1 个答案:

答案 0 :(得分:0)

您在检查"id":1时犯了什么错误,因此[{"job_id":1},{"job_id":2}]已存在于完整数组中,因此当检查条件id:1 !== job_id:2时,它会返回true,就像所有明智之举一样

检查下面的答案对我有用

/*
    $beschikbaarheid[] = (object) array("job_id" => "1"); 
    $beschikbaarheid[] = (object) array("job_id" => "2");
    $allediensten[] = (object) array("id" => "1","user_id"=>"3"); 
    $allediensten[] = (object) array("id" => "2","user_id"=>"3"); 
    $allediensten[] = (object) array("id" => "3","user_id"=>"3"); */

    $idexist=false;
    foreach($allediensten as $alledienstens)
    {
        $idexist = checkexist($alledienstens->id);
        if($idexist==false)
        {
            echo" not exist "; // Here Your code goes if "id" is not exist in "job_id"
            $idexist=false;
        }
    }
    function checkexist($job_id){
        global $beschikbaarheid;
        foreach($beschikbaarheid as $beschikbaar){
          if($beschikbaar->job_id == $job_id){
              return true;
          }
      }  
    }

您要做的是once it's exist you have to exit for further process