我有2个数组
$ data1和$ data2

当请求$ data2 is_null 时,
总是显示错误

Undefined offset: 0

在线
{{$data2[$key]->month}}

我的刀片
@foreach($data1 as $key => $value)
<tr>
  <td align="center" style="padding: 10px">{{$data1[$key]->id}}</td>
  <td align="center" style="padding: 10px">{{$data1[$key]->Parameters->name}}</td>
  <td align="center" align="center" style="padding: 10px">{{$data1[$key]->month}}</td>

 @if(isset($data2))
  <td align="center" align="center" style="padding: 10px">{{$data2[$key]->month}}</td>
 @endif
</tr>
@endforeach

我该如何解决?

最佳答案

您正在检查isset的$data3,但是您需要检查$data2[$key]

@foreach($data1 as $key => $d1)
<tr>
  <td align="center" style="padding: 10px">{{$data1[$key]->id}}</td>
  <td align="center" style="padding: 10px">{{$data1[$key]->Parameters->name}}</td>
  <td align="center" align="center" style="padding: 10px">{{$data1[$key]->month}}</td>

 @if(isset($data2[$key]))
  <td align="center" align="center" style="padding: 10px">{{$data2[$key]->month}}</td>
 @endif
</tr>
@endforeach

您也可以将$data1[$key]更改为$d1
@foreach($data1 as $key => $d1)
<tr>
  <td align="center" style="padding: 10px">{{$d1->id}}</td>
  <td align="center" style="padding: 10px">{{$d1->Parameters->name}}</td>
  <td align="center" align="center" style="padding: 10px">{{$d1->month}}</td>

 @if(isset($data2[$key]))
  <td align="center" align="center" style="padding: 10px">{{$data2[$key]->month}}</td>
 @endif
</tr>
@endforeach

关于laravel - 如果Isset无法与[$ key]配合使用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49420601/

10-12 04:14