我在这里的第一个问题,在享受了很多其他人的问题和答案之后。谢谢你:)
我正在尝试使用 vimeo 的 api,但我得到了一个我无法按我的意图使用的响应。我认为这对你们中的一些人来说很简单,但我无法理解它。
我有一堆视频 ID,需要获取其缩略图 URL。我想“询问”响应“这个 id 的缩略图 url 是什么”,然后循环遍历我所有的 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
最佳答案
您可以将该结构重新定义为一个更简单的数组以供您使用。以下假定上述结构存储在 $response
中:
$videos = array();
foreach($response->videos->video as $video) {
if(count($video->thumbnails->thumbnail) > 0) {
$videos[$video->id] = $video->thumbnails->thumbnail[0]->_content;
}
}
这将为您留下一个数组 $videos
,其中包含 id 到 url 的映射:
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 ?>
关于php - 如何从 php 中的对象/数组中获取基于另一个值的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2251467/