如果Isset没有使用[$ key]工作

时间:2018-03-22 04:31:14

标签: laravel view blade

我有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

我怎么能解决这个问题?

2 个答案:

答案 0 :(得分:1)

您正在检查$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

答案 1 :(得分:0)

试试这个,检查$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