php - 如何从 php 中的对象/数组中获取基于另一个值的值

标签 php arrays object

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

我正在尝试使用 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/

相关文章:

linux - 通过多个脚本向 Bash 数组添加数据

c# - 在 C# 中,有没有办法使 2 元素 float 组等于 Unity Vector2 变量

javascript - 使用 AJAX 提交表单后刷新 div

php - 要将我的 Codeigniter PHP Web 应用程序转换为 iOS 应用程序,我应该做的第一件事是什么?

java - 将 byte[] 转换为 UnsignedByte[]

c++ - 析构函数,返回临时对象

具有只读属性的 Javascript 对象

javascript - 显示对象内容 - JS/jQuery

php - PHP中for循环的结果随机排序

php - 为什么 mysql 中的查询有效,但 php 中的查询不起作用,这只是由于特定的列