PHP array to jquery

PHP array to jquery

I have a PHP page that grabs images from a directory and puts the file names into an Array.

I can display the images on the PHP page using a foreach loop so I know the PHP is grabbing the file names.

I script src=getImages.php on my web page but cannot get the images in the array.

After a lot of googling I think I need JSON or JSON_encode or JSON implode. But after many iterations of trial and error I'm here asking for help.

I want the image names in the array from the getImages.php to be available to work with in the javascript.

Here's some code snippets.

getImages.php
  1. <?php
  2. $path = 'images/';

  3. function getImages($path) {
  4.     $images = array();
  5.     if ( $images_dir = @opendir($path) ) {
  6.         while ( false !== ($img = readdir($images_dir)) ) {
  7.             if ( preg_match("/(\.gif|\.jpg|\.png|\.jpeg)$/", $img) ) {
  8.                 $images[] = $img;
  9.             }
  10.         }
  11.         closedir($images_dir);
  12.     }
  13.     return $images;
  14. }

  15. $imgList = getImages($path);
  16. ?>
webpage snip:
  1. <section class="roundabout">
  2.       <div class="slide">
  3. <script src="getImages.php"></script>
  4. <script>
  5. var x= $(".slide");
  6. function displayImages() {
  7. var i = 0,
  8. len = imgList.length;        
  9. for (; i < imgList.length; i++) {
  10.                                     x.append("<article><a href='" + imgList [i] + "'><img class='image featured' src='" + imgList[i] + "'></a></article>");
  11.                                    } 
  12. };
  13. $(function() {
  14. displayImages();
  15. });
  16. </script>
  17. </div>
  18. </section>
Thank you in advance!
bh