gpt4 book ai didi

php - 随机图像选择器 PHP

转载 作者:行者123 更新时间:2023-12-03 19:24:50 26 4
gpt4 key购买 nike

$images = array();
$images[0][0] = "boxes/blue.jpg";
$images[0][1] = "blah.html";
$images[1][0] = "boxes/green.jpg";
$images[1][1] = "blah.html";
$images[2][0] = "boxes/orange.jpg";
$images[2][1] = "blah.html";
$images[3][0] = "boxes/pink.jpg";
$images[3][1] = "blah.html";
$images[4][0] = "boxes/purple.jpg";
$images[4][1] = "blah.html";
$images[5][0] = "boxes/red.jpg";
$images[5][1] = "blah.html";
$images[6][0] = "boxes/yellow.jpg";
$images[6][1] = "blah.html";

$i = 0;


*echo "<a href='" . $images[0][1] . "'><img src='" . $images[0][0] . "' /></a>";

$boxes = array();
while($i<5)
{
$rand = rand(0,(sizeof($images)-1));
//echo $rand;
$slice = array_splice($images, $rand);
$boxes[$i] = $slice;
$i++;
}*

我试图让一个随机图像选择器从 $images 数组提供的图像列表中进行选择。但是,我无法用“Array”以外的任何内容填充 $boxes 数组。谁能告诉我为什么?非常感谢任何帮助

更新

我现在使用下面的代码,只要遇到空元素就会中断。除非我弄错了,否则拼接不应该修补这样的漏洞吗?

$rand = rand(0,(sizeof($images)));
array_splice($images, $rand);
$i = 0;
while($i<5)
{
echo "<a href='" . $images[$i][1] . "'><img src='" . $images[$i][0] . "' /></a>";
$i++;
}

最佳答案

这可能是一个更好的方法:

foreach (array_rand($images, 5) as $key) {
$boxes[] = $images[$key];
}

关于php - 随机图像选择器 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/280680/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com