开发者

Perform PHP loops until end of array OR reaches certain number of iterations?

开发者 https://www.devze.com 2023-04-01 03:16 出处:网络
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.

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.

For example, if I wanted to output all the 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.

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?


Use a while loop:

$i = 0;
$limit = 10;
$count = count($array);

while ($i < $limit && $i < $count) {
   $data = $array[$i];
   // your code here
   ++$i;
}


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.

$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; 


$max_iterations = 100;

for ($i=1;$i <=$max_iterations;$i++)
{
    if ($i <= count($array))
        //do what u need
    else
        break;      
}


Are you familiar with a break statement?

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.

<?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; 


            <?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.


<?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;
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消