2013-11-09 135 views
1

這涉及到這樣一個問題:Load Array via nested foreach loops in PHP?每個相冊顯示一個隨機縮略圖圖像PHP?

目前,我有這個組嵌套foreach循環:

$subfolders = glob($directory); 
foreach($subfolders as $subfolder) { 
    $photos = glob($subfolder.'/*.[Jj][Pp][Gg]'); 
    foreach($photos as $photo) { 
     $thumbnail = $subfolder.'/thumbs/'.$photoname[0].'_thumb.jpg'; 
     $thumb = '<img src="'.$thumbnail.'" class="thumb_image">'; 
    } 
} 

我希望做的是,在另一個foreach($subfolders as $subfolder)循環進一步下跌的頁面,加載一個隨機$thumb$subfolder。我試圖用一個多維數組來做到這一點,正如你在上一個問題中所看到的,但顯然這是行不通的。我還有什麼其他方法可以實現?

+0

爲什麼不給數組給唯一的id? – Esqarrouth

+0

@Esq因爲我不夠聰明,不知道該怎麼做...... – NaOH

+0

你有多少個子文件夾? – Esqarrouth

回答

1

這裏是我的意思了一個更簡潔的版本:

$subfolders = glob($directory); 
foreach($subfolders as $subfolderNo) { 
    $photos = glob($subfolderNo.'/*.[Jj][Pp][Gg]'); 
    // generate the $randomNo, random from 0 to $photo.size() 
    $thumbnail = $subfolderNo.'/thumbs/'.$photoname[$randomNo].'_thumb.jpg'; 
    $thumb = '<img src="'.$thumbnail.'" class="thumb_image">'; 
}