【发布时间】:2016-08-21 12:00:35
【问题描述】:
以下是我的 JSON,我想将所有密钥作为该节点的“节点”和“id”。
[
{
"id": 15,
"title": " Introduction",
"node": {
"id": 15,
"title": " Introduction",
"description": " Introduction on travelling abroad",
"media_info": " Introduction on travelling abroad",
"thumb_url": "test",
"media_url": "test"
},
"children": [
{
"id": 16,
"title": "Travel Preparation",
"node": {
"id": 16,
"title": "Travel Preparation",
"description": " Travel Preparation",
"media_info": "Travel Preparation",
"thumb_url": "test",
"media_url": "test"
},
"children": [
{
"id": 17,
"title": "The Act of Traveling",
"node": {
"id": 17,
"title": "The Act of Traveling",
"description": " The Act of Traveling",
"media_info": "The Act of Traveling",
"thumb_url": "/test",
"media_url": "test"
}
}
]
},
{
"id": 18,
"title": "Arrival at Your Destination Abroad",
"node": {
"id": 18,
"title": "Arrival at Your Destination Abroad",
"description": " Arrival at Your Destination Abroad",
"media_info": "Arrival at Your Destination Abroad",
"thumb_url": "test",
"media_url": "test"
},
"children": [
{
"id": 19,
"title": "In Your Destination Country",
"node": {
"id": 19,
"title": "In Your Destination Country",
"description": " In Your Destination Country",
"media_info": "In Your Destination Country",
"thumb_url": "http:test",
"media_url": "test"
}
}
]
}
]
}
]
================================================ ==========================
我正在使用下面的代码,但它没有给出正确的输出。 我想输出应该是 15,16,17,18。
$obj = json_decode($config_info, true);
foreach ($obj as $key => $value) {
print_r($value['node']['id']);
}
【问题讨论】:
-
有一些预期的结果更好。
-
id 值在此嵌套 json 中重复,因此如果我将获取节点键,那么我可以从该节点获取 id。
-
您的 Json 嵌套在不合逻辑的模式中。在此处查看以更清楚地了解问题所在:jsonviewer.stack.hu