don’t show the same images

this script show 50 random images
that change turning on themselves.
but sometimes show the same images
how don’t show the same images?

my code

<?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>";
}
    ?>

Related posts

2 comments

  1. The obvious way would be to delete a presented image from the array:

    $randomImage = $images[array_rand($images)];
    $images = array_diff($images, array($randomImage));
    
  2. Another solution: Simply use unset for removing element from array like this:

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

Comments are closed.