如果 Isset 无法使用 [$key] 进行剪裁

IF Isset not working on lopping with [$key]

我有2个数组 $data1 和 $data2

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

Undefined offset: 0 

在线

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

我的blade

@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

我该如何解决这个问题?

试试这个,检查是否设置了 $data2[$key] @if(isset($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

您正在检查 $data3 的 isset 但您需要检查 $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