2015-12-16 51 views
1

此腳本顯示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>"; 
} 
    ?> 

回答

2

明顯的方式是從數組中刪除一個呈現的圖像:

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

另一種解決方案:簡單地使用未設置用於從陣列像這樣去除元件:

$random = array_rand($images); 
$randomImage = $images[$random]; 
unset($images[$random]); 
相關問題