Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get data from an array of stdClass object

I'm trying to retrieve the value of the field from the Vimeo Api, I've tried all possible solutions mentioned here. Can someone tell me how to retrieve the thumnail and url from the thumbnails and urls object respectively?

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
                )
            )
        )
    )

I have an array $vids which has the meta info of various vids along with another call inside the loop which fetches the second array $vidInfo containing the array displayed above for each entry. I can retrieve the title etc just like I would access an object normally. but I can't traverse any further the response above.

<?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>'; 
         }
    ?>

Thanks a lot

like image 563
SinSync Avatar asked Jan 10 '23 22:01

SinSync


2 Answers

Considering you have:

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

            )

    )

Accessing an object is done via the object-access-operator (->), while accessing an array is done via square brackets ([x]). So you end up with urls->url[0]->_content in this case. Since urls is an object, and url is an array, whose first ([0]) index contains another object.

So in short, to answer your full original question: $object->urls->url[0]->_content is the URL and $object->thumbnails->thumbnail[0]->_content is the thumbnail

like image 68
Tularis Avatar answered Jan 13 '23 10:01

Tularis


For URL

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

For Thumbnail

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

As you can see from the print_r representation, you can see stdClass Object and Array , so the former represents an object and the latter represents an array. Objects are accessed like -> . For accessing array elements you use the square brackets [ ]

like image 35
Shankar Narayana Damodaran Avatar answered Jan 13 '23 11:01

Shankar Narayana Damodaran