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

How to access array nested in JSON with PHP

我有一个 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;
}

目前没有任何输出

将 menu[0] 替换为 menu,将 $starter->name 替换为 $starter[0]->name,并将 $starter->price 替换为 $starter[0]->price,如下所示:

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

如果您查看解码 JSON 字符串的 print_r($restaurant) 当您不确定 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