I have a JSON Object and I am trying to write a foreach loop to output each record in an array. This is my JSON Object code
{
"name": "Takeaway Kings",
"menu": {
"starter": [
{
"name": "Samosas",
"price": 3.5
},
{
"name": "Chaat",
"price": 1.99
}
],
"dessert": [
{
"name": "Kulfi",
"price": 2.5
},
{
"name": "Kheer",
"price": 2.99
}
],
"main": [
{
"name": "Lamb Biryani",
"price": 4.5
},
{
"name": "Chicken Tikka Masala",
"price": 5.99
}
]
}
}
and this is my PHP code
$restaurant = json_decode(file_get_contents("restaurant.json"));
$restaurant->menu[0];
foreach($starters as $starter){
$name = $starter->name;
$price = $starter->price;
//do something with it
echo $name + " . " + $price;
}
at the moment nothing is being output
$startersdefined ?$restaurant = json_decode(file_get_contents("post.php"), true);? Because that way it would be easy for you to traverse the array.