谁能帮我在php的foreach循环中使用json数据?

时间:2019-02-15 20:03:13

标签: php arrays json

我正在使用下面的php代码在文件中使用json,但我没有得到输出。我需要从Json array打印图像url。谁能帮忙。

Json数据是:

{
  "pagination": {},
  "data": [
    {
      "id": "1980025670135540608_1039191866",
      "user": {},
      "images": {
        "thumbnail": {
          "width": 150,
          "height": 150,
          "url": "https://scontent.cdninstagram.com/vp/d3d00967ec82131dedfdddb0cb8f8bc7/5CFCE4F1/t51.2885-15/e35/s150x150/50959433_383283642451967_2367872435860221844_n.jpg?_nc_ht=scontent.cdninstagram.com"
        },
        "low_resolution": {
          "width": 320,
          "height": 320,
          "url": "https://scontent.cdninstagram.com/vp/5cb939b696c2d5a0211572bd0e965bee/5CF41C01/t51.2885-15/e35/s320x320/50959433_383283642451967_2367872435860221844_n.jpg?_nc_ht=scontent.cdninstagram.com"
        },
        "standard_resolution": {
          "width": 640,
          "height": 640,
          "url": "https://scontent.cdninstagram.com/vp/411df4ade429230dee0ab3631d25a6eb/5CE56DB3/t51.2885-15/e35/50959433_383283642451967_2367872435860221844_n.jpg?_nc_ht=scontent.cdninstagram.com"
        }
      },
      "created_time": "1550257475",
      "caption": null,
      "user_has_liked": false,
      "likes": {
        "count": 0
      },
      "tags": {},
      "filter": "Normal",
      "comments": {
        "count": 0
      },
      "type": "image",
      "link": "https://www.instagram.com/p/Bt6dts6lWuA/",
      "location": null,
      "attribution": null,
      "users_in_photo": {}
    },
    {
      "id": "1979798389190546391_1039191866",
      "user": {
        "id": "1039191866",
        "full_name": "Jagjeet Kumar",
        "profile_picture": "https://scontent.cdninstagram.com/vp/9871a297c832e1b1ef249028856af412/5CFC0D7D/t51.2885-19/11931260_1465802173750145_760962573_a.jpg?_nc_ht=scontent.cdninstagram.com",
        "username": "jagjeet_k"
      },
      "images": {
        "thumbnail": {
          "width": 150,
          "height": 150,
          "url": "https://scontent.cdninstagram.com/vp/5f7e27fbf6d878bb85be23b70b637866/5CF8244F/t51.2885-15/e35/s150x150/50801938_1432836230186365_4210258679421826307_n.jpg?_nc_ht=scontent.cdninstagram.com"
        },
        "low_resolution": {
          "width": 320,
          "height": 320,
          "url": "https://scontent.cdninstagram.com/vp/16376fe1eb033705d1c7b1f394bef013/5CE38537/t51.2885-15/e35/s320x320/50801938_1432836230186365_4210258679421826307_n.jpg?_nc_ht=scontent.cdninstagram.com"
        },
        "standard_resolution": {
          "width": 640,
          "height": 640,
          "url": "https://scontent.cdninstagram.com/vp/c51878930ed8c98a595f16299f5c18d7/5CF9E7CA/t51.2885-15/sh0.08/e35/s640x640/50801938_1432836230186365_4210258679421826307_n.jpg?_nc_ht=scontent.cdninstagram.com"
        }
      },
      "created_time": "1550230381",
      "caption": null,
      "user_has_liked": false,
      "likes": {
        "count": 1
      },
      "tags": {},
      "filter": "Crema",
      "comments": {
        "count": 1
      },
      "type": "image",
      "link": "https://www.instagram.com/p/Bt5qCVCl9PX/",
      "location": null,
      "attribution": null,
      "users_in_photo": {}
    }
  ],
  "meta": {
    "code": 200
  }
}

我正在使用的Php代码:

<?php

    $instagram_feed_data = json_decode($mediacount, true);

    foreach ($instagram_feed_data->data as $item) { 

    $img_url = $item['images']['low_resolution']['url']; 

?> 

   <img src="<?= $img_url; ?>">

<?php

} 

我必须打印低分辨率数组中的图像网址。

1 个答案:

答案 0 :(得分:2)

$instagram_feed_data = json_decode($mediacount, true);

将真实值传递给json_decode()的第二个参数会告诉PHP您要返回数组而不是对象。因此,您不能使用这样的对象句柄取消引用它:

foreach ($instagram_feed_data->data as $item) { 

您想要这样的数组引用:

foreach ($instagram_feed_data['data'] as $item) {