Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 图像随机标题有问题_Php_Css_Image - Fatal编程技术网

Php 图像随机标题有问题

Php 图像随机标题有问题,php,css,image,Php,Css,Image,我想知道我能给这个随机脚本添加标题吗 <div style="background:transparent url( backgrounds/<?php echo 'image-'.rand(0,20).'.jpg'; ?>);"> Welcome to my Site! </div> 我能把两者加起来吗?可能吗?如果是,我该怎么做? 如果没有办法的话。我还能做什么?我很感激你的建议 谢谢您可以洗牌$images数组并获取第一个索引 $images =

我想知道我能给这个随机脚本添加标题吗

<div style="background:transparent url(
 backgrounds/<?php echo 'image-'.rand(0,20).'.jpg'; ?>);"> 
 Welcome to my Site!
</div>
我能把两者加起来吗?可能吗?如果是,我该怎么做? 如果没有办法的话。我还能做什么?我很感激你的建议


谢谢

您可以洗牌
$images
数组并获取第一个索引

$images = array( array('file' => 'image1', 'caption' => 'Caption 1'),
                 array('file' => 'image2', 'caption' => 'Caption 2') );

shuffle($images);
$file = $images[0]['file'];
$caption = $images[0]['caption'];

如果图像数组与您所说的类似:

$images = array(
array('file' => 'image1.jpg',
'caption' => 'Caption 1'),

array('file' => 'image2.jpg',
'caption' => 'Caption 2'),
);
然后,您可以使用
数组\u rand
获得一个随机条目:

$image_key = array_rand($images);
然后您可以使用此键为您提供图像和标题:

$images = array(array("image 1" => "caption 1"),
                array("image 2" => "caption 2"),
                array("image 3" => "caption 3");
<div style="background:transparent url(
 backgrounds/<?php echo $images[$image_key]['file']; ?>);"> 
 <?php echo $images[$image_key]['caption']; ?>
</div>

为什么您的数组看起来像这样?使用文件名作为键,使用标题作为值不是更有意义吗?(不使用2d数组)您好,这是两个独立的代码。我正在尝试为第一个代码添加标题。第二个代码有标题,但我想为第一个代码添加标题。如果我没有解释清楚,我道歉。很抱歉,我明白了,我说的是你第二段代码中的数组。为什么看起来像那样?你能改变它的格式吗?不,我的意思是这样的:
array(“image1”=>“caption 1”,“image2”=>“caption 2”,…)

根据更改是否正确。如果我写错了,我很抱歉,您需要首先确保图像具有文件扩展名!
.jpg”/>
感谢Marcus提供的详细信息,我想我忘了写文件扩展名。抱歉。嗨,马库斯,谢谢你的密码,这就是我要找的。我感谢你花时间写这篇文章。谢谢这个图像实际上更好,它不涉及数组的洗牌,这是低效的。嗨,约翰,谢谢你的回复,谢谢你的例子!我忘了那个变量数组。这是有道理的。嗨,John,所以我替换$images=array();到$image_key=array_rand();嗨,约翰,谢谢你的例子,我会测试一下,然后找到你嗨,约翰,谢谢你的例子,我会记住这些代码。我感谢你花时间写这篇文章。谢谢