Ryan Salmons
Ryan Salmons

Reputation: 1459

Why is my foreach loop not looping through and populating array?

I have a class that loops through a directory (5 images) and converts each image to base64 format and fills an array. However, it seems that the foreach loop only loops through once. There is 5 images in the directory, so it should have 5 iterations and the array should be 5 different images as well.

PHP

require_once "Results.php";
require_once "ImageHelper.php";

class IntroImageHelper {
    public static function GetImages()
    {
      $results = new Results();
      $results->IntroImages = Array();
      $dir = new DirectoryIterator("img/");
      $ImageExists = false;
      foreach($dir as $file)
      {
        if($file->isFile())
        {
          $ImageExists = $file->__toString();
          break;
        }
      }

      if($ImageExists)
      {
        $tempImage = new Results();
        $tempImage->ImageName = $ImageExists;
        $tempImage->ImageData = ImageHelperIntroSlides::DownloadImage($file);
        array_push($results->IntroImages, $tempImage);
      }

       return $results;
    }
}

Output:

{"IntroImages: [
    {"ImageName": "image.png",
     "ImageDate": "base64imagedata"
    }
  ]
}

Upvotes: 1

Views: 227

Answers (3)

Traian
Traian

Reputation: 3003

Why do you have the break in the for loop? That will quit the loop...

Upvotes: 0

meda
meda

Reputation: 45490

That's exactly what break will do, it will break out of the loop.

If you need to just flag a value, remove the break and let the iterations continue.

But in your case you did not need to, for a check change your loop to the following:

foreach($dir as $file)
{
    if($file->isFile())
    {
        $tempImage = new Results();
        $tempImage->ImageName = $file->__toString();
        $tempImage->ImageData = ImageHelperIntroSlides::DownloadImage($file);
        array_push($results->IntroImages, $tempImage);
    }
}

Upvotes: 0

Scopey
Scopey

Reputation: 6319

Using a break statement will stop loops. You can use continue to skip the rest of the execution of the current iteration and go to to the next iteration, but in your case just omitting the break will fix your problem:

foreach($dir as $file) {
    if($file->isFile()) {
      $ImageExists = $file->__toString();
    }
}

In your case, a bunch of your code is outside of the loop when it should be inside:

foreach($dir as $file) {
    if($file->isFile()) {
      $ImageExists = $file->__toString();
    }
    if($ImageExists) {
        $tempImage = new Results();
        $tempImage->ImageName = $ImageExists;
        $tempImage->ImageData = ImageHelperIntroSlides::DownloadImage($file);
        array_push($results->IntroImages, $tempImage);
    }
}

But you can also combine those two statements:

foreach($dir as $file) {
    if($file->isFile()) {
        $tempImage = new Results();
        $tempImage->ImageName = $file->__toString();
        $tempImage->ImageData = ImageHelperIntroSlides::DownloadImage($file);
        array_push($results->IntroImages, $tempImage);
    }
}

Upvotes: 4

Related Questions