限制循环在 php 中运行的次数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1998204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:36:07  来源:igfitidea点击:

limiting number of times a loop runs in php

phpforeach

提问by mrpatg

I have a foreach loop that i need to limit to the first 10 items then break out of it.

我有一个 foreach 循环,我需要将其限制为前 10 个项目,然后退出它。

How would i do that here?

我怎么会在这里?

foreach ($butters->users->user as $user) {
    $id = $user->id;
    $name = $user->screen_name;
    $profimg = $user->profile_image_url;
    echo "things";    
} 

Would appreciate a detailed explanation as well.

也希望得到详细的解释。

回答by Alex

If you want to use foreach, you can add an additional variable to control the number of iterations. For example:

如果要使用foreach,可以添加一个额外的变量来控制迭代次数。例如:

$i=0;
foreach ($butters->users->user as $user) {
    if($i==10) break;
    $id = $user->id;
    $name = $user->screen_name;
    $profimg = $user->profile_image_url;
    echo "things";  
    $i++;  
} 

回答by VolkerK

You can also use the LimitIterator.

您还可以使用LimitIterator

e.g.

例如

$users = new ArrayIterator(range(1, 100)); // 100 test records
foreach(new LimitIterator($users, 0, 10) as $u) {
  echo $u, "\n";
}

回答by Ciarán Walsh

You could simply iterate over array_slice($butters->users->user, 0, 10)(the first 10 elements).

您可以简单地迭代array_slice($butters->users->user, 0, 10)(前 10 个元素)。

回答by cletus

Use a loop counter and breakwhen you want to exit.

使用循环计数器以及break何时要退出。

$i = 0;
foreach ($butters->users->user as $user) {
  $id = $user->id;
  $name = $user->screen_name;
  $profimg = $user->profile_image_url;
  echo "things";    
  if (++$i >= 10) {
    break;
  }
} 

On the 10th iteration the loop will exit at the end.

在第 10 次迭代时,循环将在最后退出。

There are several variations of this and one thing you need to be choose is whether you want to execute the outer loop condition or not. Consider:

这有多种变体,您需要选择的一件事是是否要执行外部循环条件。考虑:

foreach (read_from_db() as $row) {
  ...  
}

If you exit at the top of that loop you will have read 11 rows. If you exit at the bottom it'll be 10. In both cases the loop body has executed 10 times but executing that extra function might be what you want or it might not.

如果您在该循环的顶部退出,您将阅读 11 行。如果你在底部退出,它将是 10。在这两种情况下,循环体都执行了 10 次,但执行那个额外的函数可能是你想要的,也可能不是。

回答by richsage

If you're sure about wanting to keep the foreachloop, add a counter:

如果您确定要保持foreach循环,请添加一个计数器:

$count = 0;
foreach ($butters->users->user as $user) {
    $id = $user->id;
    $name = $user->screen_name;
    $profimg = $user->profile_image_url;
    echo "things";    

    $count++;
    if ($count == 10)
      break;
}

so each time your loop executes, the counter is incremented and when it reaches 10, the loop is broken out of.

所以每次循环执行时,计数器都会增加,当它达到 10 时,循环就会中断。

Alternatively you may be able to rework the foreachloop to be a forloop, if possible.

或者,如果可能,您可以将foreach循环重新设计为循环for

回答by Pragati Sureka

you can start a counter before your foreach block and check against it in the loop and break if the counter is 10 like so,

您可以在 foreach 块之前启动一个计数器并在循环中检查它并在计数器为 10 时中断,就像这样,

$count = 1;
foreach ($butters->users->user as $user) {
    if($count == 10)
       break;
    $id = $user->id;
    $name = $user->screen_name;
    $profimg = $user->profile_image_url;
    echo "things";
    $count++;
} 

回答by EternalHour

I really like VolkerK's answer, but I don't understand why he is creating a new iterator when most likely you will have an existing array. Just want to share the way I ended up doing it.

我真的很喜欢 VolkerK 的回答,但我不明白他为什么要创建一个新的迭代器,而您很可能会有一个现有的数组。只是想分享我最终做的方式。

$arrayobject = new ArrayObject($existingArray);
$iterator = $arrayobject->getIterator();

foreach(new LimitIterator($iterator, 0, 10) as $key => $value) {
   // do something
}