PHP Laravel foreach从api迭代json数据
因此,我对PHP和Laravel非常陌生,为了在“analyzedInstructions”数组中获取“number”和“step”,我很难尝试进行foreach循环。我能够获得“标题”、“阅读分钟数”和“说明”,但无法获得“分析说明”中的任何内容 以下是json的dd:PHP Laravel foreach从api迭代json数据,php,json,laravel,api,foreach,Php,Json,Laravel,Api,Foreach,因此,我对PHP和Laravel非常陌生,为了在“analyzedInstructions”数组中获取“number”和“step”,我很难尝试进行foreach循环。我能够获得“标题”、“阅读分钟数”和“说明”,但无法获得“分析说明”中的任何内容 以下是json的dd: {#203 ▼ +"id": 602534 +"title": "Bok Choy with Ground Chicken Stir Fry" +"readyInMinutes": 15 +"image": "
{#203 ▼
+"id": 602534
+"title": "Bok Choy with Ground Chicken Stir Fry"
+"readyInMinutes": 15
+"image": "https://spoonacular.com/recipeImages/Bok-Choy-with-Ground-Chicken-Stir-Fry-602534.jpg"
+"instructions": "In a wok or large saute pan over high heat, add the cooking oil. When hot, add ground chicken and saute until browned (but not fully cooked through). Lower heat ▶"
+"analyzedInstructions": array:1 [▼
0 => {#558 ▼
+"name": ""
+"steps": array:4 [▼
0 => {#559 ▼
+"number": 1
+"step": "In a wok or large saute pan over high heat, add the cooking oil. When hot, add ground chicken and saute until browned (but not fully cooked through). Lower heat ▶"
+"ingredients": array:5 [▶]
+"equipment": array:2 [▶]
}
1 => {#567 ▶}
2 => {#573 ▶}
3 => {#576 ▶}
]
}
]
}
带api/密钥的控制器:
public function selectedrecipe($id){
$response = Unirest::get("https://spoonacular-recipe-food-nutrition-v1.p.mashape.com/recipes/" . $id . "/information?includeNutrition=true",
array(
"X-Mashape-Key" => " KEY ",
"Accept" => "application/json"
)
);
return view('search.show')->withSelected($response->body);
}
我正在尝试循环获取“步骤”的“编号”和“步骤”内部,并将它们列在我的刀片中
显示刀片:
@extends('layouts.app')
@section('content')
<h1>{{ $selected->title }}</h1>
<div id="recipeInfo">
<img class="recipeImg" src="{{ $selected->image }}"/>
<div class="recipeText">
<h2>Instructions</h2>
<p>Total Time: {{ $selected->readyInMinutes }} minutes</p>
<p>{{ $selected->instructions }}</p>
{{$selected->sourceUrl}}
@foreach($selected->analyzedInstructions[0]->steps[0]->number as $select)
{{$select}}
@endforeach
</div>
</div>
@endsection
@extends('layouts.app'))
@节(“内容”)
{{$selected->title}
图像}}”/>
说明书
总时间:{{$selected->readyInMinutes}}分钟
{{$selected->instructions}
{{$selected->sourceUrl}
@foreach($selected->analyzedInstructions[0]->步骤[0]->编号为$select)
{{$select}}
@endforeach
@端部
您得到的是数组的特定索引,而不是对它们进行迭代,这将导致您只打印一个结果
@foreach($selected->analyzedInstructions as $instruction)
<p>Steps:</p>
@foreach($instruction->steps as $step}}
<p>Numbers:</p>
@foreach($ste[->number as $select)
{{$select}}
@endforeach
@endforeach
@endforeach
@foreach($selected->analyzedInstructions as$instruction)
步骤:
@foreach($instruction->steps as$step}}
编号:
@foreach($ste[->编号为$select)
{{$select}}
@endforeach
@endforeach
@endforeach
这将迭代数组中的每个元素及其子元素。这可能是
@foreach($selected->analyzedInstructions[0]->步骤为$step)
,然后是{{$step->number}
或类似内容。@JoelHinz是的,成功了。谢谢!