简体   繁体   English

将foreach替换为PHP中的迭代

[英]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. 我有一个foreach循环,非常适合拉取图像,但是当我尝试将其替换为for循环时,它将破坏代码和图像。

PHP code: 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;
}

Desired solution: 所需解决方案:

To replace foreach with for and set counter in the for loop instead. 用for替换foreach并在for循环中设置counter。 This is probably really easy but for some reason I am completely stuck right now. 这可能真的很容易,但是由于某种原因,我现在完全陷入困境。

This is if your $media->data variable could be indexed. 这是如果您的$media->data变量可以建立索引。

<?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: 如果不是,则必须使用foreach而不是for并在达到所需数量的照片时退出循环:

<?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. 同样,正如在下面的注释中所写,如果图像少于5张,则最好检查$media->data变量的大小。 You can make something like: 您可以进行如下操作:

$count = (count($media->data) < 5)? count($media->data): 5;

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. 假设$media>data是一个带有数字索引的数组,则count函数和这样的索引将起作用。

But I must admit I don't know why you would do this. 但是我必须承认,我不知道你为什么要这么做。 The foreach loop is just as easy. foreach循环同样简单。

// 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>';
}
$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>';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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