从多维数组对象stdClass中检索数据

时间:2014-03-01 17:01:33

标签: php arrays

我怎样才能摆脱这种喜欢?谢谢,因为我不知道该怎么做

https://gdata.youtube.com/feeds/api/videos/f4LxBKN9ank?v=2&alt=jsonc

数组

   object(stdClass)#1 (2) {
  ["apiVersion"]=>
  string(3) "2.1"
  ["data"]=>
  object(stdClass)#2 (19) {
    ["id"]=>
    string(11) "f4LxBKN9ank"
    ["uploaded"]=>
    string(24) "2014-01-26T02:34:24.000Z"
    ["title"]=>
    string(25) "League of Legends : Worth"

    ["content"]=>
    object(stdClass)#5 (3) {
      ["5"]=>
      string(74) "https://www.youtube.com/v/f4LxBKN9ank?version=3&f=videos&app=youtube_gdata"
      ["1"]=>
      string(102) "rtsp://r5---sn-4g57kuee.c.youtube.com/CiILENy73wIaGQl5an2jBPGCfxMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp"
      ["6"]=>
      string(102) "rtsp://r5---sn-4g57kuee.c.youtube.com/CiILENy73wIaGQl5an2jBPGCfxMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp"
    }
    ["duration"]=>
    int(78)
    ["aspectRatio"]=>
    string(10) "widescreen"
    ["rating"]=>
    float(4.921824)
    ["likeCount"]=>
    string(5) "14147"
    ["ratingCount"]=>
    int(14429)
    ["viewCount"]=>
    int(678017)
  }
}

2 个答案:

答案 0 :(得分:4)

你可以这样做。

<?php
$data = @file_get_contents("http://gdata.youtube.com/feeds/api/videos/f4LxBKN9ank?v=2&alt=jsonc");
$realdata = json_decode($data);
$likecount = $realdata->data->likeCount;

答案 1 :(得分:0)

// Sample data
$data = '{"apiVersion":"2.1","data":{"id":"f4LxBKN9ank","uploaded":"2014-01-26T02:34:24.000Z","updated":"2014-03-01T16:48:02.000Z","uploader":"videogamedunkey","category":"Comedy","title":"League of Legends : Worth","description":"Allow 3-5 weeks for your burrito to arrive.\n\nhttps://www.youtube.com/watch?v=YxeOLw1npuo&list=FLsvn_Po0SmunchJYOWpOxMg&index=1","thumbnail":{"sqDefault":"https://i1.ytimg.com/vi/f4LxBKN9ank/default.jpg","hqDefault":"https://i1.ytimg.com/vi/f4LxBKN9ank/hqdefault.jpg"},"player":{"default":"https://www.youtube.com/watch?v=f4LxBKN9ank&feature=youtube_gdata_player","mobile":"https://m.youtube.com/details?v=f4LxBKN9ank"},"content":{"5":"https://www.youtube.com/v/f4LxBKN9ank?version=3&f=videos&app=youtube_gdata","1":"rtsp://r5---sn-jc47eu7e.c.youtube.com/CiILENy73wIaGQl5an2jBPGCfxMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp","6":"rtsp://r5---sn-jc47eu7e.c.youtube.com/CiILENy73wIaGQl5an2jBPGCfxMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp"},"duration":78,"aspectRatio":"widescreen","rating":4.921824,"likeCount":"14147","ratingCount":14429,"viewCount":678049,"favoriteCount":0,"commentCount":1497,"accessControl":{"comment":"allowed","commentVote":"allowed","videoRespond":"moderated","rate":"allowed","embed":"allowed","list":"allowed","autoPlay":"allowed","syndicate":"allowed"}}}';

// Decode $data into an array
$json = json_decode($data, true); 

// Get the like count from data/likeCount in the array
$likeCount = $json["data"]["likeCount"]; 

// Display the likeCount
echo $likeCount; // 14147

$json = json_decode($data, true)$data中的数据解码为名为$json的数组。该数组有一个子数组,其键为"data",该子数组包含键"likeCount"下的类似数。因此,可以通过将索引链接在一起来访问类似的计数:$likeCount = $json["data"]["likeCount"])