使用PHP迭代JSON

时间:2017-03-29 03:41:29

标签: php arrays json dictionary

我无法弄清楚如何从以下JSON获取以下键/值:

我需要在for循环中打印出来:

'项目',即'test-project-1'

'数据集'即'dataset1'

'权限',即'角色:WRITER,group:test1@test.com'

{
   "projects":{
      "test-project-1":{
         "datasets":{
            "dataset1":{
               "permissions":{
                  "role":"WRITER",
                  "group":"test1@test.com"
               }
            },
            "dataset2":{
               "permissions":{
                  "role":"READER",
                  "group":"test2@test.com"
               }
            },
            "dataset3":{
               "permissions":{
                  "role":"READER",
                  "group":"test3@test.com"
               }
            },
            "dataset4":{
               "permissions":{
                  "role":"READER",
                  "group":"test4@test.com"
               }
            },
            "dataset5":{
               "permissions":{
                  "role":"READER",
                  "group":"test5@test.com"
               }
            }
         }
      },
      "test-project-2":{
         "datasets":{
            "dataset1":{
               "permissions":{
                  "role":"WRITER",
                  "group":"test1@test.com"
               }
            },
            "dataset2":{
               "permissions":{
                  "role":"READER",
                  "group":"test2@test.com"
               }
            },
            "dataset3":{
               "permissions":{
                  "role":"READER",
                  "group":"test3@test.com"
               }
            },
            "dataset4":{
               "permissions":{
                  "role":"READER",
                  "group":"test4@test.com"
               }
            },
            "dataset5":{
               "permissions":{
                  "role":"READER",
                  "group":"test5@test.com"
               }
            }
         }
      }
   }
}

我尝试了这个,但唯一打印出来的是“数组”。

这样的事情不起作用:

$new_json = {path_to_json};
$new_projects = $new_json['projects'];
foreach ($new_projects as $project) {
    echo $project . "<br>";
    foreach ($new_projects['datasets'] as $datasets){
        echo $datasets . "<br>";
    }
}

3 个答案:

答案 0 :(得分:0)

PHP code demo

$json='{
   "projects":{
      "test-project-1":{
         "datasets":{
            "dataset1":{
               "permissions":{
                  "role":"WRITER",
                  "group":"test1@test.com"
               }
            },
            "dataset2":{
               "permissions":{
                  "role":"READER",
                  "group":"test2@test.com"
               }
            },
            "dataset3":{
               "permissions":{
                  "role":"READER",
                  "group":"test3@test.com"
               }
            },
            "dataset4":{
               "permissions":{
                  "role":"READER",
                  "group":"test4@test.com"
               }
            },
            "dataset5":{
               "permissions":{
                  "role":"READER",
                  "group":"test5@test.com"
               }
            }
         }
      },
      "test-project-2":{
         "datasets":{
            "dataset1":{
               "permissions":{
                  "role":"WRITER",
                  "group":"test1@test.com"
               }
            },
            "dataset2":{
               "permissions":{
                  "role":"READER",
                  "group":"test2@test.com"
               }
            },
            "dataset3":{
               "permissions":{
                  "role":"READER",
                  "group":"test3@test.com"
               }
            },
            "dataset4":{
               "permissions":{
                  "role":"READER",
                  "group":"test4@test.com"
               }
            },
            "dataset5":{
               "permissions":{
                  "role":"READER",
                  "group":"test5@test.com"
               }
            }
         }
      }
   }
}';
$array=  json_decode($json,true);
foreach($array["projects"] as $projectName => $projects)
{
    echo "Project name ".$projectName."<br/>";
    foreach($projects["datasets"] as $databaseNames => $databaseData)
    {
        echo "  Database name ".$databaseNames."<br/>";
        echo "  Database role ".$databaseData["permissions"]["role"]."<br/>";
        echo "  Database group ".$databaseData["permissions"]["group"]."<br/>";
    }
}

答案 1 :(得分:0)

格式为foreach(“array”为“数组中的键”)

foreach ($new_json as $new_projects) {
    echo $new_projects . "<br>";
    $datasets=$new_projects['datasets'];
    foreach ($new_projects as $datasets){
        echo $datasets . "<br>";
    }
}

答案 2 :(得分:0)

您需要使用 json_decode 函数PHP Manual

$jsonArray = json_decode($jsonData, true);
foreach($jsonArray as $project){
    echo $project . '<br>';
    foreach($project['datasets'] as $dataset)
    {
        echo $dataset . '<br>';
        foreach($dataset['permissions'] as $permissions)
        {
            echo sprintf('Role: %s, group: %s<br>', $permissions['role'], $permissions['group'];
        }
    }
}

如果您不希望将json转换为数组,则需要将false作为第二个参数传递,而将返回stdClass。