想要从 Laravel Blade 文件中的特定索引获取 JSON 文件数据

want to get json file data from particular index in laravel blade file

提问人:Timothy 提问时间:3/1/2022 最后编辑:RiggsFollyTimothy 更新时间:3/1/2022 访问量:387

问:

我是php laravel的新手,请帮助我解决这个问题。

这是json文件api

"meals": [
    {
        "idMeal": "52771",
        "strMeal": "Spicy Arrabiata Penne",
        "strDrinkAlternate": null,
        "strCategory": "Vegetarian",
        "strArea": "Italian",
        "strInstructions": "Bring a large pot of water to a boil. Add kosher salt to the boiling water, then add the pasta. Cook according to the package instructions, about 9 minutes.\r\nIn a large skillet over medium-high heat, add the olive oil and heat until the oil starts to shimmer. Add the garlic and cook, stirring, until fragrant, 1 to 2 minutes. Add the chopped tomatoes, red chile flakes, Italian seasoning and salt and pepper to taste. Bring to a boil and cook for 5 minutes. Remove from the heat and add the chopped basil.\r\nDrain the pasta and add it to the sauce. Garnish with Parmigiano-Reggiano flakes and more basil and serve warm.",
        "strMealThumb": "https://www.themealdb.com/images/media/meals/ustsqw1468250014.jpg",
        "strTags": "Pasta,Curry",
        "strYoutube": "https://www.youtube.com/watch?v=1IszT_guI08",
        "strIngredient1": "penne rigate",
        "strIngredient2": "olive oil",
        "strIngredient3": "garlic",
        "strIngredient4": "chopped tomatoes",
        "strIngredient5": "red chile flakes",
        "strIngredient6": "italian seasoning",
        "strIngredient7": "basil",
        "strIngredient8": "Parmigiano-Reggiano",
        "strIngredient9": "",
        "strIngredient10": "",
        "strIngredient11": "",
        "strIngredient12": "",
        "strIngredient13": "",
        "strIngredient14": "",
        "strIngredient15": "",
        "strIngredient16": null,
        "strIngredient17": null,
        "strIngredient18": null,
        "strIngredient19": null,
        "strIngredient20": null,
        "strMeasure1": "1 pound",
        "strMeasure2": "1/4 cup",
        "strMeasure3": "3 cloves",
        "strMeasure4": "1 tin ",
        "strMeasure5": "1/2 teaspoon",
        "strMeasure6": "1/2 teaspoon",
        "strMeasure7": "6 leaves",
        "strMeasure8": "spinkling",
        "strMeasure9": "",
        "strMeasure10": "",
        "strMeasure11": "",
        "strMeasure12": "",
        "strMeasure13": "",
        "strMeasure14": "",
        "strMeasure15": "",
        "strMeasure16": null,
        "strMeasure17": null,
        "strMeasure18": null,
        "strMeasure19": null,
        "strMeasure20": null,
        "strSource": null,
        "strImageSource": null,
        "strCreativeCommonsConfirmed": null,
        "dateModified": null
    }
]
}

这是用于获取 api 数据的控制器函数

 public function random(){
    $random1 = Http::get('www.themealdb.com/api/json/v1/1/random.php')->json();
    return view('index',['random1'=>$random1]);
}

用于显示数据的 blade 文件

 <figure>
    @foreach($random1  as $key => $randomMeal1)
       @for ($i = 1; $i < 20; $i++)
         {{$randomMeal1[0]['strMeasure1']}}
         {{$randomMeal1[0]['strIngredient1']}}
        <br>                                                            
       @endfor                                      
     @endforeach                                      
</figure>

在上面的文件中,我获取的是单个数据,但我想在一个循环中获取多个数据。

问题

我想使用循环显示从“strMeasure1”到“strMeasure15”的 api 数据。 如何使用循环..?

PHP 数组 Laravel 索引

评论

1赞 Tim Lewis 3/1/2022
{{$randomMeal1[0]["strMeasure{$i}"]}}?您在该循环中进行了硬编码,但您没有使用 .1for()$i
0赞 Tim Lewis 3/1/2022
没关系!旁注,我可能会考虑再看一下你的数据结构。每当您具有硬编码的数值属性(如 、 等)时,请考虑使用数组: 。这将使循环更容易,并且可以使你的数据更小(你的一堆数值属性是;使用数组,它们不会在那里,从而使你的JSON更小/更紧凑)thing1thing2things: [1, 2]foreach($meal->things as $thing)null

答:

-1赞 Cesar Valdes 3/1/2022 #1

尝试将响应对象转换为数组,如下所示:

{
$random1 = Http::get('www.themealdb.com/api/json/v1/1/random.php')->json();
$array_meals = $random1->meals[0];

// you can use a <for> for more data

   $meals_array_props = (array) $array_meals;
   //this convert a object into a array representation of the object
   //now you can iterate the object as an array
}