如何在PHP中随机选择数组项?

如何在PHP中随机选择数组项?,php,html,Php,Html,我正在尝试建立这个网站来练习我的编程 <html> <title>VidVoter</title> <head> <link rel="stylesheet" type="text/css" href="stylesheet.css"> <div id="welcome">Welcome to VidVoter!</div> <a hre

我正在尝试建立这个网站来练习我的编程

<html>
    <title>VidVoter</title>

    <head>
        <link rel="stylesheet" type="text/css" href="stylesheet.css">
        <div id="welcome">Welcome to VidVoter!</div>


        <a href="file://localhost/Users/alihashemi/Desktop/vidvoter.html">
            <div id="vote1">Vote for this video</div>
        </a>


        <a href="file://localhost/Users/alihashemi/Desktop/vidvoter.html">
            <div id="vote2">Vote for this video</div>
        </a>
    </head>

    <?php
    $videos = array("xrCTiImIWk4" , "WCpfVPY4J6I" , "__2ABJjxzNo" , "y7tI1E6kp0o" , "-NSL_DgwCYw"); 
    $rand = array_rand($videos);
    $rand_key = isset($_GET['id']) ? $_GET['id'] : $rand;
    ?>


    <p>

            <h1><iframe width="560" height="315" src="http://www.youtube.com/embed/<?php .$rand_key.?>" frameborder="0" allowfullscreen></iframe></h1>

            <h2><iframe id="vid2" width="560" height="315" src="http://www.youtube.com/embed/<?php .$rand_key.?>" frameborder="0" allowfullscreen></iframe><h2>


    </p>
</html>

视频投票者
欢迎来到这里!

你试过了吗

$rand_key = isset($_GET['id']) ? $_GET['id'] : $rand[0];

正如DevZer0所评论的,array_rand()返回一个数组键,而不是它的值。在您的示例中,您希望

$rand_key = isset($_GET['id']) ? $_GET['id'] : $video[$rand];

如果您只选择一个条目,array_rand()将返回随机条目的密钥。您是否安装了服务器?您使用的是文件URL,这让我相信您没有。