【问题标题】:PHP array ouput shows only one elementPHP 数组输出只显示一个元素
【发布时间】:2016-12-31 22:08:14
【问题描述】:

我正在编写一个小型 PHP 脚本,该脚本将采用 youtube id 数组并动态生成 youtube 嵌入播放列表。

我正在使用的当前 PHP 代码 - 只回显一个视频。

<iframe width="560" height="315" src="http://www.youtube.com/embed/182oUgBfoLE?&rel=0&modestbranding=1&hd=1&autohide=1&playlist=
<?php
$videos = Array("PGNiXGX2nLU", "VUJOJ0d7e8c", "o0syTUu3_S0", "rXndd78C8-c" );
// Shuffle array
shuffle($videos);
// Loop array
foreach($videos as $video);
{
    // Echo array with commas between elements
    echo "$video ,";
}
?>
" frameborder="0" allowfullscreen></iframe> 

我的问题是如何让洗牌和循环数组的所有元素得到回显?

【问题讨论】:

  • 为什么要将数组传递给 url
  • 我已经编辑了您的代码,使其更清晰。它看起来像您实际使用的代码的准确表示吗?
  • @VishnuBhadoriya 他不是。目标是为用户提供播放列表
  • 问题现在已编辑,但他在 url 中传递数组而不使用 implode
  • @VishnuBhadoriya 即使在编辑之前,他也没有将数组传递给 URL。代码难以阅读,但并非不可能;除非您指的是没有进入修订历史的编辑。

标签: php arrays video youtube


【解决方案1】:

我的看法

<?php

$videoIds = array("PGNiXGX2nLU", "VUJOJ0d7e8c", "o0syTUu3_S0", "rXndd78C8-c");
shuffle($videoIds);
$playlist = implode(',', $videoIds);

?>
<iframe width="560" height="315"
        src="http://www.youtube.com/embed/182oUgBfoLE?&rel=0&modestbranding=1&hd=1&autohide=1&playlist=<?= $playlist; ?>" 
        frameborder="0" allowfullscreen>
</iframe>

【讨论】:

    【解决方案2】:

    尝试使用此代码 -

    <?php 
    $videos = Array ( "PGNiXGX2nLU", "VUJOJ0d7e8c", "o0syTUu3_S0", "rXndd78C8-c" ); 
    // Shuffle array 
    shuffle($videos);
    $playlist = implode(',', $videos);
    ?>
    
    <iframe width="560" height="315" src="http://www.youtube.com/embed/182oUgBfoLE?&rel=0&modestbranding=1&hd=1&autohide=1&playlist=<?php echo $playlist;?>" frameborder="0" allowfullscreen></iframe>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-29
      • 1970-01-01
      • 2015-03-07
      • 1970-01-01
      • 1970-01-01
      • 2015-02-11
      • 1970-01-01
      • 2014-01-08
      相关资源
      最近更新 更多