Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/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 - Fatal编程技术网

将显示单个图片功能转换为显示多个图片功能(来自目录)php

将显示单个图片功能转换为显示多个图片功能(来自目录)php,php,Php,我想将一个函数random_pic转换为random_pics,因此该函数不显示单个图片,而是显示目录的10个随机图片 显示单个随机图片 转换为随机图片不起作用 function random_pic($dir){ $file=array(); $gifs=array(); $mdir = "../gifs/".$dir."/"; $files = glob($mdir.'*.gif'); foreach ($files as $count => $gif){ $file[$cou

我想将一个函数random_pic转换为random_pics,因此该函数不显示单个图片,而是显示目录的10个随机图片

显示单个随机图片

转换为随机图片不起作用

function random_pic($dir){
$file=array();
$gifs=array();
$mdir = "../gifs/".$dir."/";
$files = glob($mdir.'*.gif');



foreach ($files as $count => $gif){
 $file[$count] = array_rand($files);//how to?

if($count<10){
$gifs[] = " $count <img src='". $gif."' /><br/>";
}
}
$gifs=implode("",$gifs);
return $gifs;

}

如何使其工作?

正如我在评论中提到的,您需要在循环之后返回,而不是在循环内部。要完成随机化,可以使用随机播放


要么在函数中进行回显,要么每次通过循环连接到一个字符串上,然后在循环后返回该字符串。嗯…意思是?您在循环中返回。这就结束了函数,所以您只进行循环的第一次迭代。@PatrickQ请检查更新,它返回十张图片,但它们不是随机的。在循环之前,洗牌$files;删除循环中当前的第一行。
function random_pic($dir){
$file=array();
$gifs=array();
$mdir = "../gifs/".$dir."/";
$files = glob($mdir.'*.gif');



foreach ($files as $count => $gif){
 $file[$count] = array_rand($files);//how to?

if($count<10){
$gifs[] = " $count <img src='". $gif."' /><br/>";
}
}
$gifs=implode("",$gifs);
return $gifs;

}
function random_pic($dir){
    $gifs = "";
    $mdir = "../gifs/".$dir."/";
    $files = glob($mdir.'*.gif');

    // this randomizes the array of files
    suffle($files);

    foreach ($files as $count => $gif){

        if($count<10){
            // that concatenates on to gifs the string 
            $gifs .= " $count <img src='". $gif."' /><br/>";
        }
    }

    // return the final gifs string after it as been assem
    return $gifs;

}