PHP 随机图像集
我正在使用 php 脚本随机显示图像。我已经重复这个脚本三次,因为我想一次显示三个随机图像 - 我不确定如何更改 php 代码以显示 3 个图像。
问题是,我不想遇到所有三个脚本同时显示相同图像的情况。我可以在这段代码中添加一些内容来确保显示的每个图像始终不同吗?
<?php
$random = "random.txt";
$fp = file($random);
srand((double)microtime()*1000000);
$rl = $fp[array_rand($fp)];
echo $rl;
?>
html:
<?php include("rotate.php"); ?>
<?php include("rotate.php"); ?>
<?php include("rotate.php"); ?>
*random.txt 只有一个带有链接的文件名列表。
I'm using a php script to randomly show images. I've duplicated this script three times because I wanted to show three random images at once - I'm unsure of how to change the php code to show 3 images.
The problem is, I don't want to run into the chance of all three scripts showing the same images at once. Is there something that I could add to this code to make sure that each image displayed is always different?
<?php
$random = "random.txt";
$fp = file($random);
srand((double)microtime()*1000000);
$rl = $fp[array_rand($fp)];
echo $rl;
?>
the html:
<?php include("rotate.php"); ?>
<?php include("rotate.php"); ?>
<?php include("rotate.php"); ?>
*the random.txt just has a list of filenames with links.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
简单的解决方案...
需要一个rotate.php
每当代码中
每当您需要图像时
http://php.net/manual/en/function.array-pop.php
Simple solution...
rotate.php
in your code
Whenever you need an image
http://php.net/manual/en/function.array-pop.php
PS实际上,盖伦的答案更好。由于某种原因我忘记了随机播放 xD
P.S. Actually, Galen's answer is better. For some reason I forgot about shuffle xD
您可以使用 array_rand() 一次选择多个随机键,如下所示:
这样就无需多次包含该文件。这一切都可以一次性完成。
You can use
array_rand()
to select more than one random key at a time, like this:This would eliminate the need for including the file multiple times. It can all be done at once.
您可以编写一个递归函数来检查数组 ID 是否已打印,如果已打印,则再次调用自身。只需将其放入 for 循环中即可打印三次:)
但请记住,真正的随机图像可能会重叠!
You could write a recursive function to check if the array ID has already been printed, and if it has, call itself again. Just put that in a for loop to print three times :)
Though keep in mind that truly
random
images could overlap!