Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8739251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:53:50+00:00 2026-06-13T10:53:50+00:00

Problem: I have a foreach loop that works great for pulling images but when

  • 0

Problem:

I have a foreach loop that works great for pulling images but when I try to replace it with a for loop it breaks the code and images.

PHP code:

// Create counter
$i = 0;

// Set number of photos to show
$count = 5;

// Set height and width for photos
$size = '100';

// Show results
foreach ($media->data as $data) 
{
    // Show photo
    echo '<p><img src="'.$data->images->thumbnail->url.'" height="'.$size.'" width="'.$size.'" alt="Instagram bild"></p>';

    // Abort when number of photos has been reached
    if (++$i == $count) break;
}

Desired solution:

To replace foreach with for and set counter in the for loop instead. This is probably really easy but for some reason I am completely stuck right now.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:53:51+00:00Added an answer on June 13, 2026 at 10:53 am

    This is if your $media->data variable could be indexed.

    <?php
    // Create counter
    $i = 0;
    
    // Set number of photos to show
    $count = 5;
    
    // Set height and width for photos
    $size = '100';
    
    // Show results
    for ($i = 0; $i < $count; $i++) 
    {
        $data = $media->data[$i];
        // Show photo
        echo '<p><img src="'.$data->images->thumbnail->url.'" height="'.$size.'" width="'.$size.'" alt="Instagram bild"></p>';
    }
    

    If not you have to use foreach but not for and exit from the loop when you reach the needed number of photos:

    <?php
    // Create counter
    $i = 0;
    
    // Set number of photos to show
    $count = 5;
    
    // Set height and width for photos
    $size = '100';
    
    // Show results
    foreach ($media->data as $data) 
    {
        // Show photo
        echo '<p><img src="'.$data->images->thumbnail->url.'" height="'.$size.'" width="'.$size.'" alt="Instagram bild"></p>';
    
        // Abort when number of photos has been reached
        if (++$i == $count) 
            break;
    }
    

    Also as it was written in the comments below it is a good idea to check the size of your $media->data variable if there are less than 5 images. You can make something like:

    $count = (count($media->data) < 5)? count($media->data): 5;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with a continue statement in my C# Foreach loop. I
i have a problem with my code. foreach (DataRow dr in dt_pattern.Rows) { part
Problem: I have a table that prints out vertical but I would like it
I have a problem with PHP code : I have been using PHP to
I have a very strange array sorting related problem in PHP that is driving
Here is the problem: I have two columns in a table that, for each
My problem is that I have three different stock sizes for each product that
Problem: Have made a small mail program which works perfectly on my developer pc
I've seen that a few instances of this problem have been raised already. However,
I have an app that works with an idea of redemption codes (schema: ID,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.