Php Laravel Json ForeachLoop
我试图在视图中显示json文件 我的JSOn如下所示:Php Laravel Json ForeachLoop,php,json,laravel,Php,Json,Laravel,我试图在视图中显示json文件 我的JSOn如下所示: { "title": "Chilli Cheddar", "body": "Beefburger mit Cheddar Cheese, scharfem Jalapenos Relish und hausgemachte Burgersauce", "price": "7,90 €", "vendor": "Schräglage" }, { "title": "K
{
"title": "Chilli Cheddar",
"body": "Beefburger mit Cheddar Cheese, scharfem Jalapenos Relish und hausgemachte Burgersauce",
"price": "7,90 €",
"vendor": "Schräglage"
},
{
"title": "Kleiner gemischter Salat",
"body": "Beefburger mit Cheddar Cheese, scharfem Jalapenos Relish und hausgemachte Burgersauce",
"price": "3,00 €",
"vendor": "Schräglage"
},
{
"title": "Brotloser Burger mit Salat",
"body": "Burger - Brötchen und Salat",
"price": "7,90 €",
"vendor": "Schräglage"
},
{
"title": "Chicken Burger",
"body": "Burger mit herzhaftem Hähnchenbrustfilet, Tomaten, Salat, Zwiebeln und hausgemachter Burgersauce",
"price": "7,90 €",
"vendor": "Schräglage"
}
当我在阵列中循环时:
@foreach($json["dish"] as $key => $item)
@foreach($item as $key => $one)
<div class="card card-horizontal">
<div class="row">
<div class="col-md-5">
<div class="image" style="background-image: url({{asset('images/frontend/profile/profileHero.jpg')}}); background-size: cover; background-position: center center;">
<img src="{{asset('images/frontend/profile/profileHero.jpg')}}" alt="..." style="display: none;">
<div class="filter filter-azure">
<button type="button" class="btn btn-neutral btn-round">
<i class="fa fa-heart"></i> SMÄCKT MIR
</button>
</div>
</div>
</div>
<div class="col-md-7">
<div class="content">
<a class="card-link" href="#">
<h2 class="title">{{$item["title"]}}</h2>
</a>
<a class="card-link" href="#">
@if(array_has($item, 'body'))
<h4>{{$item["body"]}}</h4>
@endif
<br />
<h4 class="text-info">{{$item["price"]}}</h4>
</a>
<div class="footer">
<div class="stats">
<a class="card-link" href="#">
<i class="fa fa-male"></i> {{$item["vendor"]}}
</a>
</div>
<div class="stats">
<a class="card-link" href="#">
<i class="fa fa-comments"></i>
</a>
</div>
<div class="stats">
<a class="card-link" href="#">
<i class="fa fa-heart"></i> 231 Likes
</a>
</div>
</div>
</div>
</div>
</div>
</div>
@endforeach
@endforeach
@foreach($json[“dish”]as$key=>$item)
@foreach($key=>$one的项目)
山猫和平号
@endforeach
@endforeach
每个项目(标题、价格、正文、供应商)在我的视图中出现5次,因为我的数组中有五个键。我不确定我的循环出了什么问题。我已经尝试了几种方法,但没有成功。第二个循环不需要这是无效的json…为了扩展Charlotte所说的内容,内部的
foreach
循环不仅仅是不必要的,它实际上是导致问题的原因。为什么使用第二个循环,并且多次使用$key
变量在第二个循环中犯了错误。您应该更改第二个$key
变量。不需要第二个循环这是无效的json…要扩展Charlotte所说的内容,内部foreach
循环不仅仅是不必要的,这就是问题的真正原因。为什么使用第二个循环,并且多次使用$key
变量在第二个循环中出错。您应该更改第二个$key
变量。