每张专辑PHP显示一个随机缩略图?

时间:2013-11-09 08:40:15

标签: php arrays foreach

这与此问题有关:Load Array via nested foreach loops in 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。我尝试使用多维数组来做到这一点,你可以在我之前的问题中看到,但显然这是行不通的。我还有什么方法可以实现这个目标?

1 个答案:

答案 0 :(得分: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">';
}