1

我试图在刷新我的页面时显示一个随机的 YouTube 视频,但它不起作用。我有在这里工作的 PHP:

<?php 
$video_array = array 
('http://www.youtube.com/embed/rMNNDINCFHg', 
'http://www.youtube.com/embed/bDF6DVzKFFg', 
'http://www.youtube.com/embed/bDF6DVzKFFg'); 
$total = count($video_array); 
$random = (mt_rand()%$total); 
$video = "$video_array[$random]"; 
?>

我试图把它放进去:

<iframe width='1006' height='421' src='<?php echo $video; ?>' frameborder='0'       allowfullscreen></iframe>

但它似乎不起作用。你们能帮帮我吗?谢谢!我一直在寻找 2 个小时,并得到了同样糟糕的结果。

4

4 回答 4

2
<?php 
$video_array = array 
('http://www.youtube.com/embed/rMNNDINCFHg', 
'http://www.youtube.com/embed/bDF6DVzKFFg', 
'http://www.youtube.com/embed/bDF6DVzKFFg'); 
shuffle($video_array);
$video = $video_array[0]; 
?>

然后你可以嵌入它。

<iframe width='1006' height='421' src='<?php echo $video; ?>' frameborder='0'       allowfullscreen></iframe>
于 2013-09-12T23:49:08.650 回答
0

不知道你为什么这样做:

$video = "$video_array[$random]"; 

让它看起来像这样

$video = $video_array[$random];
于 2013-09-12T23:47:47.537 回答
0

我只关心单行:x

<?php echo $video_array[rand(0,(count($video_array)-1))]; ?>

到目前为止,其他答案并没有减少 count() ......当它抓取最高数字时,这将导致错误,因为开始 index==0

于 2013-09-12T23:49:45.063 回答
0

试试这个代码...

<?php 
$video_array = array 
('http://www.youtube.com/embed/rMNNDINCFHg', 
'http://www.youtube.com/embed/bDF6DVzKFFg', 
'http://www.youtube.com/embed/bDF6DVzKFFg'); 
$total = count($video_array); 
$random = rand(0, $total-1); 
$video = $video_array[$random]; 
?>
于 2013-09-13T00:06:16.033 回答