我看了一下,但我不确定我能找到一个具体的答案,所以我很抱歉,如果我错过了一个合适的帖子,请把我链接到它:)
我正在创建一个脚本来搜索文件夹中更多的文件夹,并将每个文件夹中的所有文件列表编译成一个多数组。文件夹名称将是随机的,所以我有点不知道如何使用foreach循环提取它们。
我也对"[0] => array "中的第二个数组感到困惑。目前脚本的输出数据如下所示:
Array
(
[0] => Array
(
[folders] => Array
(
[album 1] => Array
(
[0] => wallpaper-1089283.jpg
[1] => wallpaper-2549931.png
[2] => wallpaper-379108.jpg
[3] => wallpaper-38130.jpg
)
[album 2] => Array
(
[0] => ns75FqFLk1v6jEmecgu25IOk8yB.jpg
[1] => wallpaper-1627330.jpg
[2] => wallpaper-1629402.jpg
)
[album 3] => Array
(
[0] => wallpaper-1042083.jpg
[1] => wallpaper-176023.jpg
[2] => wallpaper-2624435.jpg
)
)
)
)
所以我真的想让这些数据输出到一些html,如下所示。我还没有真正做多数组的工作,我希望我能学习:)
<div>
<img src="image.jpg"/>
<img src="image.jpg"/>
<img src="image.jpg"/>
</div>
<div>
<img src="image.jpg"/>
<img src="image.jpg"/>
<img src="image.jpg"/>
</div>
<div>
<img src="image.jpg"/>
<img src="image.jpg"/>
<img src="image.jpg"/>
</div>
foreach ($array[0]['folders'] as $albumID => $images) {
... <div> ...
foreach($images as $img) {
.... <img> here ...
}
.... </div>
}
foreach($array[0]['folders'] as $albumarray)
{
echo "<div>";
foreach(albumarray as $img)
{
echo '<img src="'.$img.'"/>'
}
echo "</div>";
}
试试这个:
foreach($data as $singleFolder) {
echo '<div>';
foreach($singleFolder as $img){
echo "<img src='$img' />";
}
echo '</div>'
}
$array = array(
0 => array(
'folders' => array(
'album 1' => array(
0 => 'wallpaper1.jpg',
2 => 'wallpaper1.jpg',
3 => 'wallpaper1.jpg',
),
'album 2' => array(
0 => 'wallpaper1.jpg',
2 => 'wallpaper1.jpg',
3 => 'wallpaper1.jpg',
),
'album 3' => array(
0 => 'wallpaper1.jpg',
2 => 'wallpaper1.jpg',
3 => 'wallpaper1.jpg',
)
)
)
);
$output = '';
foreach ($array[0]['folders'] as $key => $value) {
$output .= '<div>n';
foreach ($value as $file) {
$output .= 't<img src="'.$file.'" />n';
}
$output .= '</div>n';
}
echo $output;
试试这样做。
$albums = $array[0]['folders'];
foreach ($album as $img_array)
{
echo "<div>";
foreach ($img_array as $img)
{
echo "<img src='".$img."'/>";
}
echo "</div>";
}