0

I have an array of images being displayed from a specific directory using the following code:

<?php 
    $dir = "img";
    $files = scandir($dir); 
    echo '<pre>', htmlspecialchars(print_r($files, true)), "</pre>\n";  
?>

The output of this code is as follows:

Array
(
    [0] => .
    [1] => ..
    [2] => image_1.png
    [3] => image_2.png
    [4] => image_3.png
)

What I would like to do is display the array of images as shown below thus removing [x] => for each image and also removing Array ( ).

image_1.png
image_2.png
image_3.png

Im not sure how to go about this. I have never really worked with arrays before therefore the only methods I can think of are preg_replace() or str_replace(), but honestly not sure where I would start.

Any help would be kindly appreciated.

1

1 Answer 1

1

Try:

echo '<pre>', htmlspecialchars(join("\n", $files)), "</pre>\n";
Sign up to request clarification or add additional context in comments.

3 Comments

thank you for the quick and simple response. works like a charm. will accept answer when I'm allowed to.
And quick way to remove . and ..: $files = array_diff( scandir($dir), array('.', '..') );
@Fabio: much appreciated. just to let you know, you forgot a single quote within the array ;)

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.