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