执行 PHP 循环直到数组结束或达到一定的迭代次数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7274392/
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
Perform PHP loops until end of array OR reaches certain number of iterations?
提问by Joe W
I wish to receive one array as input, filter values from it, and output as another array. The function should loop through up to x
iterations.
我希望接收一个数组作为输入,从中过滤值,然后作为另一个数组输出。该函数应该循环直到x
迭代。
For example, if I wanted to output allthe values from the input, I would use:
例如,如果我想从输入中输出所有值,我会使用:
<?php
$i=0;
foreach ($array as $data) {
if ($data['type'] != 'some_value') {
$formatted_array[$i] = $data;
$i++;
}
}
return $formatted_array;
But if $array
had a large index, the $formatted_array
would be larger than I need. I tried using a for
loop with multiple conditions, but it seems to get stuck in an infinite loop.
但是如果$array
有一个大的索引,它$formatted_array
会比我需要的大。我尝试使用for
具有多个条件的循环,但它似乎陷入了无限循环。
I'm not a developer by trade, so the logic is difficult to comprehend. I'm not getting errors, so it's hard to understand where exactly I'm going wrong.
我不是贸易开发人员,所以逻辑很难理解。我没有收到错误,所以很难理解我到底哪里出错了。
How can I perform PHP loops until end of array or until the function reaches certain number of iterations?
如何执行 PHP 循环直到数组结束或函数达到一定的迭代次数?
回答by meagar
Use a while
loop:
使用while
循环:
$i = 0;
$limit = 10;
$count = count($array);
while ($i < $limit && $i < $count) {
$data = $array[$i];
// your code here
++$i;
}
回答by Michael Berkowski
You're on the right track - you can exit the foreach
loop when you reach your count. You use a foreach
to iterate over the full array and if you never reach your stated maximum count, you will process the whole array. But if you do reach the maximum, jump out of the loop.
你在正确的轨道上 -foreach
当你达到你的计数时,你可以退出循环。您使用 aforeach
遍历整个数组,如果您从未达到规定的最大计数,您将处理整个数组。但是,如果您确实达到了最大值,请跳出循环。
$i = 0;
// Don't allow more than 5 if the array is bigger than 5
$maxiterations = 5;
foreach ($array as $data) {
if ($i < $maxiterations) {
if ($data['type'] != 'some_value') {
$formatted_array[$i] = $data;
$i++;
}
}
else { // Jump out of the loop if we hit the maximum
break;
}
}
return $formatted_array;
回答by Chirayu Vyas
<?php foreach($usersinglevideodata1 as $userkey=>$uservalue){
for($i = 0; $i < 5; $i++){if($uservalue['com_video'.$i.''] !=""){ if ($Count < 3){?>
<div class="col-sm-4">
<iframe width="100%" height="120" src="https://www.youtube.com/embed/<?php echo youtube_id($uservalue['com_video'.$i.'']);?>" frameborder="0" allowfullscreen></iframe>
</div>
<?php $Count++;}else{break;}}}?>
<?php }}?>
Note: You're on the right track - you can exit the foreach loop when you reach your count. You use a foreach to iterate over the full array and if you never reach your stated maximum count, you will process the whole array. But if you do reach the maximum, jump out of the loop.
注意:您在正确的轨道上 - 当您达到计数时,您可以退出 foreach 循环。您使用 foreach 迭代整个数组,如果您从未达到规定的最大计数,您将处理整个数组。但是,如果您确实达到了最大值,请跳出循环。
回答by xbonez
$max_iterations = 100;
for ($i=1;$i <=$max_iterations;$i++)
{
if ($i <= count($array))
//do what u need
else
break;
}
回答by Shamim Hafiz
Are you familiar with a break
statement?
你熟悉一个break
声明吗?
User foreach loop and also maintain a counter variable every time you enter the code inside condition. If you reach the required number elements, that is counter reaches a certain value, break out of the loop.
每次在条件中输入代码时,用户 foreach 循环并维护一个计数器变量。如果达到所需的元素个数,即计数器达到一定值,则跳出循环。
<?php
$i=0;
foreach ($array as $data) {
if ($data['type'] != 'some_value') {
$formatted_array[$i] = $data;
$i++;
if($i>$maxAllowedElements) // here $i can serve as the counter and you can define //$maxAllowedElements to whatever value you like.
break;
}
}
return $formatted_array;
回答by DenMette
<?php
$counter = 0;
$new_array = array();
while(count($new_array) <= $max_elements) {
if($array[$counter]['type'] !== 'some_value') {
$new_array[] = $array[$counter];
}
}
return $new_array;
?>