Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.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/3/arrays/12.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/6/rest/5.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_Arrays_Scandir - Fatal编程技术网

Php 阵列随机图像

Php 阵列随机图像,php,arrays,scandir,Php,Arrays,Scandir,因此,我有一段代码,可以从提供的阵列中随机拍摄图像: $bg = array('1.jpg', '2.jpg', '2.jpg',); // array of filenames $i = rand(0, count($bg)-1); // generate random number size of the array $selectedBg = "$bg[$i]"; // set variable equal to which random filename was chosen 但我想

因此,我有一段代码,可以从提供的阵列中随机拍摄图像:

$bg = array('1.jpg', '2.jpg', '2.jpg',); // array of filenames

$i = rand(0, count($bg)-1); // generate random number size of the array
$selectedBg = "$bg[$i]"; // set variable equal to which random filename was chosen
但我想从一个文件夹中获取图像,无论名称和数量如何

我试过这个:

$dir = "bg_photos/less_saturation/";

$exclude = array( ".","..");
if (is_dir($dir)) {
    $files = scandir($dir);
    foreach($files as $file){
        if(!in_array($file,$exclude)){
            echo $file;         
            $bg = array($file); // array of filenames
            $i = rand(0, count($bg)-1); // generate random number size of the array
            $selectedBg = "$bg[$i]"; // set variable equal to which random filename was chosen
        }
    }
}
但这只给了我阵列中的最后一张图像。。。 有人能帮忙吗

干杯


克里斯

你可以这样做:

$dir = "bg_photos/less_saturation/";

if (is_dir($dir)) {
    $files = array_diff(scandir($dir), array('..', '.'));

    $i = rand(0, count($files)-1); // generate random number size of the array
    $selectedBg = $files[$i]; // set variable equal to which random filename was chosen
}

您可以使用此代码。这是将所有文件(包括
)收集到一个数组中,并从数组中随机获取一个项

$dir = "bg_photos/less_saturation/";
$exclude = array( ".","..");
$bg = array();
if (is_dir($dir)) {
    $files = scandir($dir);
    foreach($files as $file){
        if(!in_array($file,$exclude)){
            echo $file;         
            //Use as an array
            $bg[] = $file; // array of filenames
        }
    }
}
$selectedBg = $bg[array_rand($bg)];

技巧1:打印$bg数组,技巧2:读取并与数组$bg=array('1.jpg','2.jpg','2.jpg',)进行比较;向数组添加1项使用$bg[]=$FILE这不是随机的。它将始终返回最后一个。@lolka_bolka为什么只返回最后一个?我遗漏了什么吗?很酷,这最后一行是我一直在寻找的,非常感谢,我会在几分钟内接受你的问题:)但是,在你的循环之外做它。并使用
$bg[]
代替
$bg=
,因为您总是会重写它。