首页 > 解决方案 > 打印嵌套 JSON 中的值

问题描述

当我尝试打印嵌套循环的值时,我收到以下错误严重性:通知消息:尝试访问 null 类型值的数组偏移量。下面是我的json。

 $json_string='{​
    "items": [
                {​
                    "quantity": "1",
                    "combinations": [
                        {​
                            "quantity": 1,
                            "lineTotal": "0.0",
                            "price": 0.0,
                            "combinationName": "ABC"
                        }​,
                        {​
                            "quantity": 1,
                            "lineTotal": "16.0",
                            "price": 16.0,
                            "combinationName": ""
                        }​,
                        {​
                            "quantity": 1,
                            "lineTotal": "0.0",
                            "price": 0.0,
                            "combinationName": " DEF"
                        }​
                    ],
                    "price": "0.0",
                    "menuItemName": "Classic Hummus",
                    "menuItemId": "37300"
                }​,
                {​
                    "quantity": "1",
                    "combinations": [
                        {​
                            "quantity": 1,
                            "lineTotal": "0.0",
                            "price": 0.0,
                            "combinationName": " HIJ"
                        }​,
                        {​
                            "quantity": 1,
                            "lineTotal": "0.0",
                            "price": 0.0,
                            "combinationName": " KLM"
                        }​,
                        {​
                            "quantity": 1,
                            "lineTotal": "0.0",
                            "price": 0.0,
                            "combinationName": ""
                        }​
                    ],
                    "price": "16.0",
                    "menuItemName": "Classic Hummus",
                    "menuItemId": "37300"
                }​,
                {​
                    "quantity": "1",
                    "combinations": [
                        {​
                            "quantity": 1,
                            "lineTotal": "0.0",
                            "price": 0.0,
                            "combinationName": "Egg, Avo & Hummus"
                        }​,
                        {​
                            "quantity": 1,
                            "lineTotal": "0.0",
                            "price": 0.0,
                            "combinationName": "Chicken Tikka & Curry Hummus Wrap"
                        }​,
                        {​
                            "quantity": 1,
                            "lineTotal": "0.0",
                            "price": 0.0,
                            "combinationName": ""
                        }​,
                        {​
                            "quantity": 1,
                            "lineTotal": "0.0",
                            "price": 0.0,
                            "combinationName": "Chicken Musakhan & Hummus"
                        }​,
                        {​
                            "quantity": 1,
                            "lineTotal": "0.0",
                            "price": 0.0,
                            "combinationName": ""
                        }​,
                        {​
                            "quantity": 1,
                            "lineTotal": "0.0",
                            "price": 0.0,
                            "combinationName": "Chicken Shawarma & Hummus Wrap"
                        }​,
                        {​
                            "quantity": 1,
                            "lineTotal": "0.0",
                            "price": 0.0,
                            "combinationName": ""
                        }​,
                        {​
                            "quantity": 1,
                            "lineTotal": "0.0",
                            "price": 0.0,
                            "combinationName": "Supergreen Falafel & Hummus Wrap"
                        }​,
                        {​
                            "quantity": 1,
                            "lineTotal": "0.0",
                            "price": 0.0,
                            "combinationName": ""
                        }​
                    ],
                    "price": "93.0",
                    "menuItemName": "Wraps Bundle For 5",
                    "menuItemId": "37328"
                }​
            ]
 }';

下面是我的代码。当我尝试打印嵌套循环的值时,我得到严重性:通知消息:尝试访问 null 类型值的数组偏移量。下面是我的代码。

$obj = json_decode($json_string,true);
$order_contents = $obj['items'];

  foreach($order_contents as $order) {
    $menuItemName = $order['menuItemName'];
    $price = $order['price'];
    $combinations = $order['combinations'];

    foreach($combinations as $combination) {
      echo $menuItemName.'-'.$combination['combinationName'].'-'.$price.'.';
    }
   }

标签: phpjson

解决方案


推荐阅读