我想在PHP中以相同的正确顺序对2个循环(或函数)进行洗牌。
目前的代码如下:
<div class="overlay">
<?php
// Get the list of all files with .jpg extension in the directory and save it in an array named $images
$directory = "uploads/_gallery/*.jpg";
// Create an array of images found in the uploads directory
$images = glob($directory);
// Randomise/shuffle the images
shuffle($images);
foreach( $images as $image ):
if ($image) { ?>
<div class="slide">
<?php echo "<img src='" . $image . "'>"; ?>
</div>
<? }
endforeach;
?>
</div>
<div class="grid">
<?php
// Get the list of all files with .jpg extension in the directory and save it in an array named $images
$directory = "uploads/_gallery/thumbs/*.jpg";
// Create an array of images found in the uploads directory
$images = glob($directory);
// Randomise/shuffle the images
shuffle($images);
foreach( $images as $image ):
if ($image) { ?>
<figure class="unit_25">
<?php echo "<img src='" . $image . "'>"; ?>
</figure>
<? }
endforeach;
?>
</div>
我尝试创建两个单独的图像数组并将它们与Shuffle合并,但没有运气。
答案 0 :(得分:1)
我假设该文件在两个目录中都具有相同的名称。
所以随机播放$images
,然后使用basename($images)
获取文件名:
$directory = "uploads/_gallery/*.jpg";
$images = glob($directory);
shuffle($images);
// For full sized images
foreach ($images as $image) {
// Your stuff
}
// For thumbnails
foreach ($images as $image) {
$basename = basename($image);
$thumbnailPath = "uploads/_gallery/thumbs/". $basename;
// Your stuff
}