下面是我的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']);
}
答案 0 :(得分:2)
您可以尝试使用array_walk_recursive功能:
javax.swing.Action newFolder = fileChooser.getActionMap().get("New Folder");
newFolder.setEnabled(false);
javax.swing.Action home = fileChooser.getActionMap().get("Home");
home.setEnabled(false);
javax.swing.Action upOneLevel = fileChooser.getActionMap().get("Up One Level");
upOneLevel.setEnabled(false);
答案 1 :(得分:1)
在您的代码中,$value['node']['id']
引用['node']['id']
键,因此它只会为您提供一个输出。
在你想要别人的时候,你也必须获取“儿童”键。比如这样:
$obj = json_decode($json, true);
foreach ($obj as $key => $value) {
echo $value['node']['id']; // return 15
$children = $value['children'];
if(is_array($children)){
foreach ($children as $child){
if(is_array($child['children'])){
foreach($child['children'] as $secondLevelChild){
echo $secondLevelChild['id'];// return 17 and 19;
}
}
echo $child['id']; // return 16 and 18
}
}
}
您可以使用相同的方法在17
键的第二级检索children
结果,请参阅上面的代码。
答案 2 :(得分:1)
检查出来:
$arr = json_decode($json, true);
$ids = array();
function get_ids($arr){
global $ids;
foreach($arr as $key => $value){
if($key == 'node')
$ids[] = $value['id'];
if(is_array($value))
get_ids($value);
}
}
get_ids($arr);
echo '<pre>';
print_r(array_unique($ids));
<强>结果:强>
Array
(
[0] => 15
[2] => 16
[4] => 17
[6] => 18
[7] => 19
)