开发者

How to get a value based on another value from a object/array in php

My first question here, after enjoying lots og other peoples questions and answers. Thanks for that :)

Im trying to work with vimeo's api, and im getting a response I can't figure out to use as I intend. I think its simple for some of you guys, but can't wrap my head around it.

I got a bunch of video id's that I need to get the thumbnail url for. I would like to "ask" the response "What's the thumb url for this id" and loop though all my id's like that.

The response object looks like this in a stripped down version:

stdClass Object
(
    [videos] => stdClass Object
        (
            [video] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 8128888
                            [thumbnails] => stdClass Object
                                (
                                    [thumbnail] => Array
                                        (
                                            [0] => stdClass Object
                                                (
                                                    [height] => 75
                                                    [width] => 100
                                                    [_content] => http://ts.vimeo.com.s3.amazonaws.com/370/931/37093137_100.jpg
                                                )
                                        )
                                )
                        )
                    [1] => stdClass Object
                        (
                            [id] => 8760295
                            [thumbnails] => stdClass Object
                                (
                                    [thumbnail] => Array
                                        (
                                            [0] => stdClass Object
                                                (
                                                    [height] => 75
                                                    [width] => 100
                                                    [_content] => http://ts.vimeo.com.s3.amazonaws.com/417/146/41714684_100.jpg
                                                )
                                        )
                                )
                        )
                )
        )
)

I need to use it like this (again stripped down), its the quoted part 开发者_Go百科I cant figure out:

<?php while ( $vid->fetchRecord() ) :
    $vid_id = $vid->get_field('video');
    $thumb = "find [video][thumbnail][0][_content] where [video][id] = $vid_id";
?>
<img src="<?php echo $thumb;?>" 
<?php endwhile ?>

So, how close am I? :P


You could redefine that structure into a simpler array for your use. The following assumes the structure above is stored in $response:

$videos = array();
foreach($response->videos->video as $video) {
  if(count($video->thumbnails->thumbnail) > 0) {
    $videos[$video->id] = $video->thumbnails->thumbnail[0]->_content;
  }
}

That will leave you with an array $videos of id to url mappings:

Array(
  '8128888' => 'http://ts.vimeo.com.s3.amazonaws.com/370/931/37093137_100.jpg',
  '8760295' => 'http://ts.vimeo.com.s3.amazonaws.com/417/146/41714684_100.jpg'
)

Your output code would then just need to be:

<?php while ( $vid->fetchRecord() ) :
    $vid_id = $vid->get_field('video');
    $thumb = $videos[$vid_id];
?>
<img src="<?php echo $thumb;?>" 
<?php endwhile ?>


// Assuming the response you posted above is stored in the variable $response

$thumbnails = array ( );

foreach ( $response->videos->video as $vID => $v )
{
        $thumbnails [ $vID ] = $v->thumbnails->thumbnail[0]->_content;
}


If i'm correct the first part of code you've entered may correspond to executing something like this:

var_dump($responseObject);

So following that assumption you can try and get a thumbnails for all returned videos like that:

<?php foreach($responseObject->videos->video as $video) { ?>
    <img src="<?= $video->thumbnails->thumbnail[0]->_content?>" />
<?php } ?>

This is what I think you try to accomplish with your while() loop.


You could walk the above posted object/arrays like this:

// $vid is the object you posted above
foreach( $vid->videos->video as $key => &$video ) {
    echo "id: " . $video->id;
    echo "thumbnails: \n";
    foreach( $video->thumbnails->thumbnail as $key => $thumb ) {
        echo "thumb {$key}: <img src=\" " . $thumb->_content . " \"> \n";
    }
}

There are simpler ways, if you don't need to care wheter there is only one or multiple thumbnails.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜