通过在PHP中嵌套的foreach循环载荷阵? [英] Load Array via nested foreach loops in PHP?

查看:99
本文介绍了通过在PHP中嵌套的foreach循环载荷阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想产生一个数组,看起来像这样:

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">'
)

现在我有一个看起来像这样两个嵌套的foreach循环:

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?

最后,我想这样做是有从每张专辑一个单一的随机缩略图进一步呼应下跌的一页......如果有人能详细阐述,以及如何做,我会很感激,但如果我能获取数组的工作,我想怎么那么我也许可以弄清楚如何做到这一点(我知道我需要使用 array_rand())。

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()).

谢谢!

推荐答案

PHP中的数组确实是一个键和值之间的映射。关键必须是唯一的。因此,你不能

An array in PHP is really a mapping between a key and a value. The key has to be unique. Therefore you cannot have

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

您需要考虑另一种数据结构,以满足你的需要。

You need to think of another data structure to meet you needs

这篇关于通过在PHP中嵌套的foreach循环载荷阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆