kexxcream
kexxcream

Reputation: 5933

Replace foreach with for iteration in PHP

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.

Upvotes: 0

Views: 2341

Answers (3)

GolezTrol
GolezTrol

Reputation: 116100

If you determine the right count before entering the loop, you can save yourself a check on each iteration, seperating the initialization code from the loop code.

The count function and indexing like this will work, assuming $media>data is an array with a numeric index.

But I must admit I don't know why you would do this. The foreach loop is just as easy.

// Set number of photos to show
$count = count($media->data);
if ($count > 5)
    $count = 5;
// Set height and width for photos
$size = '100';

// Show results
for ($i = 0; $i < $count; $i++)
{
    // Use $i as an index to get the right item.
    $data = $media->data[$i];
    echo '<p><img src="'.$data->images->thumbnail->url.'" height="'.$size.'" width="'.$size.'" alt="Instagram bild"></p>';
}

Upvotes: 1

enenen
enenen

Reputation: 1967

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;

Upvotes: 2

Mihai Matei
Mihai Matei

Reputation: 24276

$limit = 5;

for($i = 0; $i < count($media->data) && $i < $limit; $i++) {
    echo '<p><img src="'.$media->data[$i]->images->thumbnail->url.'" height="'.$size.'" width="'.$size.'" alt="Instagram bild"></p>';
}

Upvotes: 0

Related Questions