如何根据php中对象/数组的另一个值获取值

时间:2010-02-12 11:37:07

标签: php arrays object

我的第一个问题是,在享受了许多其他人的问题和答案之后。谢谢你们:)

我正在尝试使用vimeo的api,并且我得到了一个我无法按照我的意图使用的响应。我认为对你们中的一些人来说很简单,但不能把它包裹起来。

我有一堆视频ID,我需要获取缩略图网址。我想“问”回答“这个id的拇指网址是什么”,然后循环我所有的id就像那样。

响应对象在精简版本中如下所示:

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

我需要像这样使用它(再次剥离),引用的部分我无法弄清楚:

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

那么,我有多近? :P

4 个答案:

答案 0 :(得分:2)

您可以将该结构重新定义为更简单的数组供您使用。以下假设上述结构存储在$response

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

这会给你留下一个id $videos的数组到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'
)

您的输出代码只需要:

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

答案 1 :(得分:1)

// 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;
}

答案 2 :(得分:0)

如果我纠正你输入的代码的第一部分可能对应于执行这样的事情:

var_dump($responseObject);

因此,按照这种假设,您可以尝试为所有返回的视频获取缩略图:

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

这是我认为您尝试使用while()循环完成的。

答案 3 :(得分:0)

你可以像这样走过上面发布的对象/数组:

// $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";
    }
}

如果你不需要关心只有一个或多个缩略图,有更简单的方法。