简体   繁体   中英

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"
    }
  ]
}

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);
    }
}

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

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM