1

I had a look around but I'm not sure I can find a specific answer so I apologise if I've missed a suitable post, please link me to it :)

I'm creating a script to search a folder for more folders, and compile a list of all the files in each folder into a multi-array. The folder names will be random so I'm a little lost on how to extract them using a foreach loop.

I'm also confused about the second array in "[0] => Array". My output data from my script currently looks like this:

    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
                        )

                )

        )

)

So really I want to get that data to output to some html like below. I haven't really done much work with multi arrays and I'm hoping I can learn :)

<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>
1

5 Answers 5

2
foreach ($array[0]['folders'] as $albumID => $images) {
    ... <div> ...
   foreach($images as $img) {
      .... <img> here ...
   }
   .... </div>
}
Sign up to request clarification or add additional context in comments.

4 Comments

Why not just a recursive function, in case OP has files in multiple subfolders?
Then the OP should mention that. I'm not going to build an overly complicated "quick and dirty" example if it's not needed.
@DainisAbols Yumm! recursive functions
Luckily it won't be recursive :P Hence not mentioning it. Thank you all!
0
foreach($array[0]['folders'] as $albumarray)
{
  echo "<div>";
   foreach(albumarray as $img)
   {
    echo '<img src="'.$img.'"/>'
   }
   echo "</div>";
}

Comments

0

Try this:

foreach($data as $singleFolder) {
  echo '<div>';
   foreach($singleFolder as $img){
     echo "<img src='$img' />";
   }
  echo '</div>'
}

Comments

0
$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;

Comments

0

Try something like this.

$albums = $array[0]['folders'];
foreach ($album as $img_array)
{
  echo "<div>";
    foreach ($img_array as $img)
    {
       echo "<img src='".$img."'/>";
    }
  echo "</div>";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.