0

I am trying to generate an array that looks like this:

Array
(
  [Album1] => '<img src="album1_thumb1.jpg">'
  [Album1] => '<img src="album1_thumb2.jpg">'

  [Album2] => '<img src="album2_thumb1.jpg">'
  [Album2] => '<img src="album2_thumb2.jpg">'
)

Right now I have two nested foreach loops that look like so:

$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">';
        $folderthumbs[$subfolder] .= $thumb;
    }
}

This doesn't do exactly what I want, though, as it basically creates an array that looks like this:

Array
(
  [Album1] => '<img src="album1_thumb1.jpg"><img src="album1_thumb2.jpg">'

  [Album2] => '<img src="album2_thumb1.jpg"><img src="album2_thumb2.jpg">'
)

How can I correct this?

Ultimately, what I would like to do is to have one single random thumbnail from each album echoed further down the page... If someone could elaborate as well on how to do that I'd be grateful, though if I can get the Array working how I'd like then I can probably figure out how to do that (I know I need to use array_rand()).

Thanks!

4

1 回答 1

1

PHP 中的数组实际上是键和值之间的映射。密钥必须是唯一的。因此你不能拥有

  [Album1] => '<img src="album1_thumb1.jpg">'
  [Album1] => '<img src="album1_thumb2.jpg">'

您需要考虑另一种数据结构来满足您的需求

于 2013-11-09T08:20:19.767 回答