使用PHP遍历JSON文件

时间:2018-11-16 00:56:31

标签: php json loops

下面是我的json文件,我需要访问其中的某些部分。我试图遍历json文件,然后回显所选内容。

{
  "Userprofile": [
    {
      "profile": {
        "profile_type": "Customer",
        "users": [
          {
            "external_urls": {
              "ThereWebsite": "https://www.example.com/58lV9VcRSjABbAbfWS6skp"
            },
            "id": "58lV9VcRSjABbAbfWS6skp",
            "name": "Example Name",
            "type": "Customer",
            "uri": "www.test.com"
          }
        ],
        "available_markets": [
          "AD",
          "AR",
          "AT",
          "AU",
          "AD"
        ],
        "external_urls": {
          "spotify": "https://www.test.com"
        },
        "href": "https://www.test.com",
        "id": "5uU2uM1RGHfzlA12opjqol",
        "images": [
          {
            "height": 637,
            "url": "https://i.scdn.co/image/03e6e600fb3b9c4c6ef0df6b0d287d0e7d18d799",
            "width": 640
          },
          {
            "height": 299,
            "url": "https://i.scdn.co/image/c6f43aeb1beba2eb216a175be31e4ce0fb7fe094",
            "width": 300
          },
          {
            "height": 64,
            "url": "https://i.scdn.co/image/28e08d0ce4a749f33e04590eb6225d30985c004f",
            "width": 64
          }
        ]
}

我如何遍历使用php来访问此数据并将其显示在我的网页上

当前使用

"images": [
          {
            "height": 637,
            "url": "https://i.scdn.co/image/03e6e600fb3b9c4c6ef0df6b0d287d0e7d18d799",
            "width": 640
          },
          {
            "height": 299,
            "url": "https://i.scdn.co/image/c6f43aeb1beba2eb216a175be31e4ce0fb7fe094",
            "width": 300
          },
          {
            "height": 64,
            "url": "https://i.scdn.co/image/28e08d0ce4a749f33e04590eb6225d30985c004f",
            "width": 64
          }
        ]

我用于以前的json文件的示例,但是它的文件要小得多。

   foreach($result as $album)
            {
                foreach($album['artists'] as $artist)
                {
                  echo "Name: " . $artist['name'] . ", URL: " . $artist['external_urls']['spotify']   .  "\r\n";

                }
         }

0 个答案:

没有答案