简体   繁体   中英

PHP echo out progress of a nested foreach loop with each iteration

I have the following nested foreach loop. While this is executing (it's a biggie) I want follow progress by printing out the output of each iteration as it executes, rather than waiting for it to complete. Just a raw echo would be fine, or even better send the output to a view using AJAX. I'm using Laravel 5.3.

Artist 1:

  • Release 1
  • Release 2
  • Release 3
  • ...

Artist 2:

  • Release 1
  • Release 2
  • Release 3
  • ...

...

$artists = Artist::all();

foreach($artists as $artist) {

    ...get_data...

     $releases = $data->releasees;

    foreach ($releases as $release) {

        $artist_release = new ArtistRelease;

        artist_release->title = $release->title;

        $artist_release->save();

    }
}

Cheers!

Content from PHP is typically not displayed until the script has finished running, therefore it's not as simple as running an echo.

You'll need to look at your setup of this operation and make this run as a background task, which you could then query separately. You can check out this SO question for ideas on how to achieve that.

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