php - 打印嵌套 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.'.';
}
}
解决方案
推荐阅读
- python - python ValueError:形状(389,1)的不可广播输出操作数与广播形状(389,7)不匹配
- node.js - 如何使用 Spotify API 上传自定义播放列表封面图片
- laravel - 如何从集合 laravel 中检索值
- r - 使用 (ref:foo) 格式化 bookdown 定理环境标题(例如,示例)
- php - 检查 isset 的特定值
- c++ - 具有原始类型的 C++ 并发队列
- ios - List 对象的 SwiftUI 分页
- angular - Angular 8:错误错误:未捕获(承诺中):TypeError:您提供了一个无效的对象,其中预期有流
- linux - 将循环索引(增量)的值分配给bash中的变量
- angular - 锚标记上的角度(单击)不起作用