0

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

3
  • 2
    Where is $starters defined ? Commented Jan 20, 2016 at 15:13
  • Did you see the error message this code generates Commented Jan 20, 2016 at 15:19
  • Is there any reason why you didn't use $restaurant = json_decode(file_get_contents("post.php"), true);? Because that way it would be easy for you to traverse the array. Commented Jan 20, 2016 at 15:23

2 Answers 2

2

If you look at a print_r($restaurant) of the decodes JSON string always a good start point when you are not sure of the JSON syntax, you will see what structure it has.

stdClass Object
(
    [name] => Takeaway Kings
    [menu] => stdClass Object
        (
            [starter] => Array
                (
                    [0] => stdClass Object
                        (
                            [name] => Samosas
                            [price] => 3.5
                        )

                    [1] => stdClass Object
                        (
                            [name] => Chaat
                            [price] => 1.99
                        )

                )

            [dessert] => Array
                (
                    [0] => stdClass Object
                        (
                            [name] => Kulfi
                            [price] => 2.5
                        )

                    [1] => stdClass Object
                        (
                            [name] => Kheer
                            [price] => 2.99
                        )

                )

            [main] => Array
                (
                    [0] => stdClass Object
                        (
                            [name] => Lamb Biryani
                            [price] => 4.5
                        )

                    [1] => stdClass Object
                        (
                            [name] => Chicken Tikka Masala
                            [price] => 5.99
                        )
                )
        )
)

Also in PHP the concatenation character is . and not +

$restaurant = json_decode(file_get_contents("restaurant.json"));

print_r($restaurant);

foreach($restaurant->menu->starter as $starter){
   echo $starter->name . ' = ' . $starter->price . PHP_EOL;
}

Will produce the output

Samosas = 3.5
Chaat = 1.99
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you this explanation helped me a lot
1

Replace menu[0] with menu and $starter->name with $starter[0]->name and $starter->price with $starter[0]->price like this:

$restaurant = json_decode(file_get_contents("restaurant.json"));
$starters = $restaurant->menu;

foreach($starters as $starter){
   $name = $starter[0]->name;
   $price = $starter[0]->price;
   //do something with it
   echo $name + " . " + $price;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.