Laravel foreach有可能指数不同
我有一个循环,其中有索引更改的对象。正如你在第一个物体上看到的,我们有电视,第二个有gv。问题是,我想用laravel提供的blade foreach循环遍历所有对象。(此数据作为数组发送,我只是将其作为JSON进行读取) 这是我可以从逻辑上得出的结果,但我仍然返回了未定义的索引:tvLaravel foreach有可能指数不同,laravel,loops,foreach,laravel-5.3,Laravel,Loops,Foreach,Laravel 5.3,我有一个循环,其中有索引更改的对象。正如你在第一个物体上看到的,我们有电视,第二个有gv。问题是,我想用laravel提供的blade foreach循环遍历所有对象。(此数据作为数组发送,我只是将其作为JSON进行读取) 这是我可以从逻辑上得出的结果,但我仍然返回了未定义的索引:tv @foreach($products as $product) <tr> <td>{{$product['items']['pv']}}</td> <td
@foreach($products as $product)
<tr>
<td>{{$product['items']['pv']}}</td>
<td>
@if( empty($product['items']['tv']) )
{{$product['items']['gv']}}
@else
{{$product['items']['tv']}}
@endif
</td>
<td>{{$product['items']['ov']}}</td>
</tr>
@endforeach
@foreach($products作为$product)
{{$product['items']['pv']}
@if(空($product['items']['tv']))
{{$product['items']['gv']}
@否则
{{$product['items']['tv']}
@恩迪夫
{{$product['items']['ov']}
@endforeach
您的代码对我来说似乎很合适,为什么不将$product和$products的内容转储到foreach声明的正下方,看看有什么内容?返回的数组都很好。我可以通过移除中间的td来运行代码,而无需尝试显示tv或gv。它显示良好。您知道empty()将返回true吗?因为对于unset、null、0、false等,empty为true。。。也许您想使用isset或数组\u密钥\u存在?另外,如果你发布被转储的数组也会很好,因为你不能发布真实的数组。公司数据。我会努力的谢谢!谢谢数组\键\存在有效
@foreach($products as $product)
<tr>
<td>{{$product['items']['pv']}}</td>
<td>
@if( empty($product['items']['tv']) )
{{$product['items']['gv']}}
@else
{{$product['items']['tv']}}
@endif
</td>
<td>{{$product['items']['ov']}}</td>
</tr>
@endforeach
@if( array_key_exists('tv',$product['value']) )
{{$product['value']['tv']}}
@else
{{$product['value']['gv']}}
@endif