PHP:如何用array_rand显示几个随机图像?

时间:2016-05-01 12:55:14

标签: php

我使用此脚本从包含子文件夹的文件夹中显示一个随机图片:

<?php
$imagesDir = glob('folders/pics/*', GLOB_ONLYDIR);
$randomfolder = $imagesDir[array_rand($imagesDir)];
$images = glob($randomfolder . '/*.{jpg,jpeg,png,gif}', GLOB_BRACE);
$randomImage = $images[array_rand($images)];
echo '<img src="'.$randomImage.'" class="image">'; 
?>

一切正常!但是现在我想同时显示5张图片(对于一个caroussel-slider)。我使用了以下代码

$randomImage = $images[array_rand($images, 5)];

但它向我显示了这个警告:

  

警告:非法偏移类型[...]

我做错了什么?

3 个答案:

答案 0 :(得分:2)

您使用<div class="row" id="featuredcities"> <div class="col-sm-3"> <img id="image" src="images/singapore.jpg" alt="singapore"> <a href="#"><p id="text">Singapore</p></a> <p id="summarytext">Amazing culinary experience and <br>efficient business ecosystem in this <br>tiny cosmopolitan country.</p> </div> <div class="col-sm-3"> <img id="image" src="images/bangkok.jpg" alt="bangkok"> <a href="#"><p id="text">Bangkok</p></a> <p id="summarytext">A bustling neon-lit city that combines tradition and modernity.</p> </div> <div class="col-sm-3"> <img id="image" src="images/shanghai.jpg" alt="shanghai"> <a href="#"><p id="text">Shanghai</p></a> <p id="summarytext">Blend of East meets West <br>in this high energy metropolis.</p> </div> <div class="col-sm-3"> <img id="image" src="images/hcmc.jpg" alt="ho chi minh city"> <a href="#"><p id="text">HCMC</p></a> <p id="summarytext">The "Paris of Asia", Ho Chi Minh City is <br>as much historical as it is modern.</p> </div> </div> <!--End of 1st div row--> <!--2nd div row--> <div class="row" style="padding-top: 50px"> <!-- padding top is all you need --> <div class="col-sm-3"> <img id="image" src="images/seoul.jpg" alt="seoul"> <a href="#"><p id="text">Seoul</p></a> <p id="summarytext">Famous for pop culture, <br>vibrant shopping and <br>historical palaces.</p> </div> <div class="col-sm-3"> <img id="image" src="images/bangkok.jpg" alt="bangkok"> <a href="#"><p id="text">Bangkok</p></a> <p id="summarytext">A bustling neon-lit city that combines tradition and modernity.</p> </div> <div class="col-sm-3"> <img id="image" src="images/shanghai.jpg" alt="shanghai"> <a href="#"><p id="text">Shanghai</p></a> <p id="summarytext">Blend of East meets West <br>in this high energy metropolis.</p> </div> <div class="col-sm-3"> <img id="image" src="images/hcmc.jpg" alt="ho chi minh city"> <a href="#"><p id="text">HCMC</p></a> <p id="summarytext">The "Paris of Asia", Ho Chi Minh City is <br>as much historical as it is modern.</p> </div> </div> <!--End of 2st div row--> 功能错误。它将返回array_rand()个键,而不是单个数字。你能做的是:

array
  

但是,如果您的$randomImageKeys = array_rand($images, 5); for ($randomImageKeys as $key) { echo '<img src="'.$images[$key].' class="image">'; } 数组包含的内容较少,那么冒着 E_WARNING 的风险   超过5张图片 - 为避免这种情况,您可以使用以下内容:

$images

答案 1 :(得分:1)

$randomImageIndexes = array_rand($images, 5);

foreach ($randomImageIndexes as $imageIndex){

   echo $images[$imageIndex];

}

答案 2 :(得分:0)

由于array_rand($array, $n)返回一个数组$n > 1(c.f。php.net documentation : array_rand),因此您必须遍历结果才能使脚本正常工作。

它应该这样正常工作:

<?php
$imagesDir = glob('folders/pics/*', GLOB_ONLYDIR);
$randomfolder = $imagesDir[array_rand($imagesDir)];
$images = glob($randomfolder . '/*.{jpg,jpeg,png,gif}', GLOB_BRACE);
foreach(array_rand($images) as $imageIndex)
    echo '<img src="'.$images[$imageIndex].'" class="image">';