php foreach转java,Foreach循环PHP错误

在尝试使用PHP的`foreach`循环遍历从YouTube API获取的评论数据时,遇到了'试图获取非对象的属性'的错误。代码中,从API获取JSON数据并进行了解析,然后尝试访问`ytdata->items[0]->snippet->topLevelComment->snippet->textDisplay`,但在循环中直接使用`textDisplay`可能导致了错误。
摘要由CSDN通过智能技术生成

我想使用foreach循环“textDisplay”,但我不确定我的PHP代码的哪一部分是错误的,它给了我一个“试图获取非对象的属性”错误

XML

 
 

{

"kind": "youtube#commentThreadListResponse",

"etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/o6YjewN3UppKqc9x-ZYYa5xYhA8\"",

"pageInfo": {

"totalResults": 9,

"resultsPerPage": 20

},

"items": [

{

"kind": "youtube#commentThread",

"etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/uE9QsmedbKmEauRAmmwW18vNQa8\"",

"id": "z12qxfxr2onpy1b5l04cdfzrgwabir0q4bo",

"snippet": {

"videoId": "Au87oAJ2jeE",

"topLevelComment": {

"kind": "youtube#comment",

"etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/EUV0UwLw788gwYsvyDO2xMRjG8w\"",

"id": "z12qxfxr2onpy1b5l04cdfzrgwabir0q4bo",

"snippet": {

"authorDisplayName": "Randy Taschner",

"authorProfileImageUrl": "https://yt3.ggpht.com/--vE0X3_vDCs/AAAAAAAAAAI/AAAAAAAAAAA/P6kgycrPEZw/s28-c-k-no-mo-rj-c0xffffff/photo.jpg",

"authorChannelUrl": "http://www.youtube.com/channel/UCTRuBHRb4BRFcob-hMj6NnQ",

"authorChannelId": {"value": "UCTRuBHRb4BRFcob-hMj6NnQ"},

"videoId": "Au87oAJ2jeE",

"textDisplay": "Thank you Dan and Envato for creating this video!",

"textOriginal": "Thank you Dan and Envato for creating this video!",

"canRate": true,

"viewerRating": "none",

"likeCount": 1,

"publishedAt": "2015-08-16T05:02:25.000Z",

"updatedAt": "2015-08-16T05:02:25.000Z"

}

},

"canReply": true,

"totalReplyCount": 1,

"isPublic": true

}

}

]

}

我的PHP代码

$json = file_get_contents('https://www.googleapis.com/youtube/v3/commentThreads?part=snippet%2Creplies&videoId='.$videoid.'&key='.$apikey);

$ytdata = json_decode($json);

foreach($ytdata->items[0]->snippet->topLevelComment->snippet->textDisplay as $hit){

echo $hit;

}

谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值