来自阵列PHP5的随机图像

来自阵列PHP5的随机图像,php,Php,这个数组是正确创建的,虽然我需要选择一个,然后打印它,这对我来说是不可能的…有什么想法吗 <?php $bgimagearray = array(); $iterator = new DirectoryIterator("/home/sites/yellostudio.co.uk/public_html/public/themes/yello/images/backgrounds"); foreach ($iterator as $fileinfo) { if ($fileinfo

这个数组是正确创建的,虽然我需要选择一个,然后打印它,这对我来说是不可能的…有什么想法吗

<?php
$bgimagearray = array();
$iterator = new DirectoryIterator("/home/sites/yellostudio.co.uk/public_html/public/themes/yello/images/backgrounds");
foreach ($iterator as $fileinfo) {
    if ($fileinfo->isFile() && !preg_match('/-c\.jpg$/', $fileinfo->getFilename())) {
        $bgimagearray[] = "'" . $fileinfo->getFilename() . "'";
    }
}

$bgimage = array_rand  ( $bgimagearray, 2 );


?>
<img src="<?php echo URL_PUBLIC; ?>public/themes/yello/images/backgrounds/<?php echo $bgimage; ?>" alt=""/>

使用PHP手册:

如果只拾取一个条目,array_rand()将返回随机条目的键。否则,它将返回随机项的键数组。这样做是为了您可以从数组中拾取随机键和值

您将2作为第二个参数传递,因此将获得一个密钥数组,在
中用作字符串

要解决这个问题,您必须编写如下内容:

<?php
// ...
$bgimage = array_rand  ( $bgimagearray);
?>
<img src="<?php echo URL_PUBLIC; ?>.../<?php echo $bgimagearray[$bgimage]; ?>" alt=""/>

…/“alt=”“/>