从stdClass对象数组中获取数据

时间:2014-03-26 12:07:39

标签: php api object vimeo stdclass

我试图从Vimeo Api中检索该字段的值,我已经尝试了这里提到的所有可能的解决方案。有人能告诉我如何分别从缩略图和网址对象中检索thumnail和url?

array(1) {
    [0]=>       
        stdClass Object (
            [allow_adds] => 1
            [embed_privacy] => anywhere
            [id] => 123456789
            [is_hd] => 0
            [is_transcoding] => 0
            [license] => 0
            [privacy] => anybody
            [title] => Soap Opera
            [description] => 
            [upload_date] => 2014-02-20 03:03:50
            [modified_date] => 2014-02-20 19:06:05
            [number_of_plays] => 1
            [number_of_likes] => 0
            [number_of_comments] => 0
            [width] => 600
            [height] => 480
            [duration] => 32
            [owner] => stdClass Object (
                [display_name] => blah
                [id] => 12345678
                [is_plus] => 0
                [is_pro] => 1
                [is_staff] => 0
                [profileurl] => http://vimeo.com/st
                [realname] => ST
                [username] => ST
                [videosurl] => http://vimeo.com/st/videos
                [portraits] => stdClass Object (
                    [portrait] => Array (
                        [0] => stdClass Object (
                            [height] => 30
                            [width] => 30
                            [_content] => http://b.vimeocdn.com/x.jpg
                        )

                        [1] => stdClass Object (
                            [height] => 75
                            [width] => 75
                            [_content] => http://b.vimeocdn.com/x.jpg
                        )

                        [2] => stdClass Object (
                            [height] => 100
                            [width] => 100
                            [_content] => http://b.vimeocdn.com/x.jpg
                        )

                        [3] => stdClass Object (
                            [height] => 300
                            [width] => 300
                            [_content] => http://b.vimeocdn.com/x.jpg
                        )

                    )

                )

            )

            [urls] => stdClass Object (
                [url] => Array (
                    [0] => stdClass Object (
                        [type] => video
                        [_content] => http://vimeo.com/0000
                    )
                )
            )

        [thumbnails] => stdClass Object (
            [thumbnail] => Array (
                [0] => stdClass Object (
                    [height] => 75
                    [width] => 100
                    [_content] => http://b.vimeocdn.com/x.jpg
                )
            )
        )
    )

我有一个数组 $ vids ,其中包含各种视频的元信息以及循环内的另一个调用,该调用获取包含上面显示的数组的第二个数组 $ vidInfo 每个条目。我可以检索标题等,就像我正常访问一个对象一样。 但我无法再进一步了解上面的回应。

<?php 
     $vids = $videos->videos->video;    
         foreach ($vids as $vid){
             $id = $vid->id;
             $vidInfo = $vimeo->call('vimeo.videos.getInfo', array('video_id' => $id));
             $vidUrl = $vidInfo->video;  
             echo  $vid->title;
             echo '<br />';
             print_r($vidUrl->urls->url[0]->{'_content'});
             //echo '<pre>' . print_r($vidUrl) .  '</pre>'; 
         }
    ?>

非常感谢

2 个答案:

答案 0 :(得分:6)

考虑到你:

    [urls] => stdClass Object
    (
        [url] => Array
            (
                [0] => stdClass Object
                    (
                        [type] => video
                        [_content] => http://vimeo.com/0000
                    )

            )

    )

通过object-access-operator(->)访问对象,而访问数组则通过方括号([x])完成。 因此,在这种情况下,您最终会使用urls->url[0]->_content。由于urls是一个对象,url是一个数组,其第一个([0])索引包含另一个对象。

简而言之,要回答完整的原始问题: $object->urls->url[0]->_content是网址 和 $object->thumbnails->thumbnail[0]->_content是缩略图

答案 1 :(得分:2)

对于网址

echo $yourobj->urls->url[0]->{'_content'};

缩略图

echo $yourobj->thumbnails->thumbnail[0]->{'_content'};

正如您从print_r表示中看到的那样,您可以看到stdClass ObjectArray,因此前者代表object,后者代表array }。像->一样访问对象。要访问数组元素,请使用方括号[ ]