Php don';不显示相同的图像

Php don';不显示相同的图像,php,wordpress,Php,Wordpress,此脚本显示50个随机图像 这种变化本身就在发生。 但有时会显示相同的图像 如何不显示相同的图像 我的代码 <?php $all_images = glob("wp-content/themes/mysite/img-company/{*.jpg, *.JPG, *.JPEG, *.png, *.PNG}", GLOB_BRACE); $images = glob("wp-content/themes/mysite/img-company/{*.jpg, *.JPG, *.

此脚本显示50个随机图像 这种变化本身就在发生。 但有时会显示相同的图像 如何不显示相同的图像

我的代码

<?php
        $all_images = glob("wp-content/themes/mysite/img-company/{*.jpg, *.JPG, *.JPEG, *.png, *.PNG}", GLOB_BRACE);

$images = glob("wp-content/themes/mysite/img-company/{*.jpg, *.JPG, *.JPEG, *.png, *.PNG}", GLOB_BRACE);
shuffle($all_images);



foreach ($all_images as $index => $image ) {
     if ($index == 50) break;  // Only print 50 images
     $image_name = basename($image);
     $randomImage = $images[array_rand($images)];
     echo "<li><img src='/wp-content/themes/mysite/img-company/{$image_name}' /><img src='/$randomImage' /></li>";
}
    ?>

显而易见的方法是从阵列中删除显示的图像:

$randomImage = $images[array_rand($images)];
$images = array_diff($images, array($randomImage));

另一种解决方案:只需使用unset从数组中删除元素,如下所示:

$random = array_rand($images);
$randomImage = $images[$random];
unset($images[$random]);