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.

Lodder
  • 19,758
  • 10
  • 59
  • 100
  • http://stackoverflow.com/questions/1215799/how-to-get-the-pictures-from-folder-one-by-one-and-display-in-the-page-using-php You can do something like this. – Matt Jul 31 '12 at 22:17

1 Answers1

1

Try:

echo '<pre>', htmlspecialchars(join("\n", $files)), "</pre>\n";
Ross Smith II
  • 11,799
  • 1
  • 38
  • 43
  • thank you for the quick and simple response. works like a charm. will accept answer when I'm allowed to. – Lodder Jul 31 '12 at 22:19
  • 1
    And quick way to remove `.` and `..`: `$files = array_diff( scandir($dir), array('.', '..') );` – Fabio Mora Jul 31 '12 at 22:21
  • @Fabio: much appreciated. just to let you know, you forgot a single quote within the array ;) – Lodder Jul 31 '12 at 22:24