如何使用PHP访问嵌套在JSON中的数组

时间:2016-01-20 15:11:36

标签: php arrays json jsonobject

我有一个JSON对象,我正在尝试编写一个foreach循环来输出数组中的每个记录。这是我的JSON对象代码

{
  "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
      }
    ]
  }
}

这是我的PHP代码

$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;
}

目前没有任何输出

2 个答案:

答案 0 :(得分:3)

如果您查看print_r($restaurant)解码JSON字符串,当您不确定JSON语法时始终是一个很好的起点,您将看到它具有的结构。

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
                        )
                )
        )
)

同样在PHP中,串联字符为.而不是+

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

print_r($restaurant);

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

会产生输出

Samosas = 3.5
Chaat = 1.99

答案 1 :(得分:1)

将菜单[0]替换为菜单,使用$ starter->名称替换$ starter [0] - >名称和$ starter->价格与$ starter [0] - >价格如下:

$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;
}