0

I'm trying to loop through the first 5 items of an array that contain a specific value.

The code below causes an infinite loop.

$i = 0;
while ($i < 5):
    if ($counselor[$i]->state == $state || !$state): 
       // do stuff
       $i++; 
    endif;
endwhile;

Essentially I want to end the loop after the if statement has run 5 times.

5
  • 1
    @gogaz yep, I like to call it "longhand", I find it especially useful in files that also contain HTML Commented Aug 10, 2018 at 10:13
  • #jeremy, based on your comments, I can see that you have a misunderstanding with the concept of conditional statements i.e. if-else blocks. Commented Aug 10, 2018 at 10:20
  • @Arvind - I understand loops and else blocks. What I can't wrap my head around is running an if statement exactly 5 times. Commented Aug 10, 2018 at 10:22
  • #jeremy, consider debugging var_dump($counselor[$i]->state,$state) without using if-else block inside the loop for let say while(count($counselor)) Commented Aug 10, 2018 at 10:26
  • 1
    If your if statement is false the first time ($i = 0) it will never match...since $i always will be 0. Commented Aug 10, 2018 at 10:26

7 Answers 7

4

While others have explained why your solution currently does not work, and some ways around it, the best alternative is to loop the entire array until you find 5 matches - by using a foreach-loop instead.

By using an foreach-loop, you will never run into issues if the array has less than 5 matching elements (if it has less than 5 matching elements, it will never break).

$i = 0;
// Loop the array
foreach ($counselor as $k=>$v) {
    // Check if there is a match
    if ($v->state == $state || !$state) {
        // Do whatever if a match here
        $i++;
    }
    // If we have found 5 matches, break out of the loop!
    if ($i == 5) {
        break;
    }
}

You can now check how big $i is, and if less than 5, you found less than 5 matches. If it's exactly 5, you found your matches, and ended the loop.

Sign up to request clarification or add additional context in comments.

Comments

2

If the if statement is not met, then you will only stay checking the $counselor[0]->state.

You need a separate counter for when the if statement is met.

$i = 0;
$containsCount = 0;
while ($containsCount < 5 || !isset($counselor[$i])):

    if ($counselor[$i]->state == $state || !$state):
        // do stuff
        ++$containsCount;   
    endif;
    ++$i;

endwhile;

I've also added a bounds check by checking if the $counselor[$i] is null. (could also check $i < $arrayLength)

Comments

1

If your if statement is false the first time ($i = 0) it will never match...since $i always will be 0. The same goes for $i = 1, 2, 3 or 4. If any of those are false, the loop will be stuck. $i will never increase. You need another solution.

Comments

0

You increment $i only when the if statement returns true. If you take your $i++; out of the if statement the loop will always run exactly 5 times.

$i = 0;
while($i < 5):

    if($counselor[$i]->state == $state || !$state): 
       // do stuff
    endif;
    $i++; 

endwhile;

3 Comments

This runs the while loop 5 times, regardless of how many times the if loop runs. I need to run the if loop exactly 5 times.
Then your original code was correct. But it is not a save way of doing it. You have a chance that your if statement is never true and therefore never gets to 5. This will indeed cause an infinite loop.
Just looking at your code again, is it an option to change the while loop to a for loop? Where you run through the whole $counselor array and then have a break after 5 successful if statements or otherwise it will stop once you reach the end of the $counselor array.
0

You have to exit $i++ of your if condition :

$i = 0;
while($i < 5):

    if($counselor[$i]->state == $state || !$state): 
       // do stuff
    endif;
    $i++; 

endwhile;

2 Comments

This runs the while loop 5 times, regardless of how many times the if loop runs. I need to run the if loop exactly 5 times.
You got an infinite loop because you never pass into your if condition. You have to limit the while to avoid infinite loop. Are you sure then you r if condition is not always false?
0

if the if() condition is not met 5 times, it will be infinite as the while() condition will never be met. Instead of while statement, use a loop that matches the length of the array and it will end then when it reaches the end of the array.

Comments

0

Maybe you can try altering your if statement a bit like this :

$i = 0;
while($i < 5):

    if($counselor[$i++]->state == $state || !$state): 
       // do stuff
    endif;

endwhile;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.