【问题标题】:PHP For each displaying all the results 10 at a timePHP 对于每个一次显示所有结果 10
【发布时间】:2017-02-23 03:40:47
【问题描述】:

目前我正在使用 YouTube's API 返回视频的相关视频,它返回 15 个结果,内容如下。

get https://www.googleapis.com/youtube/v3/search?part=snippet&relatedToVideoId='.$id.'&type=video&maxResults=16&key='.$key.'

但是我怎样才能在 foreach 中返回这些数据,而不是仅仅打印它,如下所示。

print '<pre>';
    print_r($related);
print '</pre>';

我不是开发人员,但我正在努力学习。我希望它显示 10 个结果,然后显示更多选项,这将显示另外 10 个。

我从上面的代码中得到的结果如下。 http://pastebin.com/5ikPeFQm

【问题讨论】:

  • 你能分享你在结果中收到的数据吗?
  • print_r($related); 的输出是什么?
  • @mith 更新了帖子,我删除了很多结果,因为在到达50
  • @Wayward 这个结果不包含。的东西,你想用 foreach 显示哪一个?
  • @SahilGulati 每个帖子的标题

标签: php youtube


【解决方案1】:

使用 foreach() 循环获取项目:

foreach($related['items'] as $item){

 echo 'videoId --->'.$item['id']['videoId'];
 echo '<br>';
 echo 'publishedAt--->'.$item['snippet']['publishedAt'];
 echo '<br>';
 echo 'title--->'.$item['snippet']['title'];
 echo '<br>';
 echo 'description--->'.$item['snippet']['description'];

}


if(isset($related['nextPageToken'])){
    echo '<a href="url?nextPageToken='.$related['nextPageToken'].'">load more</a>';
}

【讨论】:

  • 这将显示我的结果直到 15,谢谢!但是,您能否将我链接到某个地方,让我了解如何添加一个显示更多按钮,该按钮将在按下时显示接下来的 15 个结果?
  • 查看我的编辑..,希望对您有所帮助。还设置 maxResults=15 以获得 15 个结果@Wayward
【解决方案2】:

希望这是必需的结果。

foreach($related["items"] as $data)
{
    echo "Title: ".$data["snippet"]["title"]."\n";
}

【讨论】:

    【解决方案3】:

    如果您的链接数组或字典看起来像:

    $Urls = array ("http 1" ,"http 2" ,"http 3","http 4" ,"http 5",
                  "http 6" ,"http 7" ,"http 8" ,"http 9" ,"http 10",
                  "http 11", "http 12","http 13","http 14","http 15",
                  "http 16" ,"http 17" ,"http 18" ,"http 19" ,"http 20",
                  "http 21" ,"http 22" , ".....etc");
    

    然后你像这样调用你的链接源数组:

      $Outeveryten = array_chunk($Urls , 10);
    
      foreach  ($Outeveryten as $result ) {
    
        foreach ($result as $values ) 
        print $values . " <br> ";  
        echo "<br> __________________ <br>"; 
        }
    

    现在结果将如您所愿(每 10 个结果显示一次):


    http 1
    http 2
    http 3
    http 4
    http 5
    http 6
    http 7
    http 8
    http 9
    http 10


    http 11
    http 12
    http 13
    http 14
    http 15
    http 16
    http 17
    http 18
    http 19
    http 20


    http 21
    http 22
    .....等等


    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-17
      • 2017-07-29
      • 2017-12-29
      • 1970-01-01
      • 1970-01-01
      • 2016-12-05
      • 2021-02-16
      • 1970-01-01
      相关资源
      最近更新 更多