2

I have a snippet of PHP code like this:

  while($attempts < $cycles) {

        foreach($player_array as $player) {
                if(isset($player['results']) && $player['results'] == 1) {
                        $player['results'] = player_search($f, $s, $t);
                }
        }
}

The idea is for each item in the array it calls a search function. If the search has nothing, it returns false, which SHOULD stop it from calling the search for the rest of the remaining cycles by changing the value of 'results'.

I'm assuming that setting $player['results'] as false does not actually set the correct variable for when it pulls it in on the next iteration.

How should i change my code to get this working properly?

EDIT:

Can I make clear that if a search in a function returns no results I do not want that function to be called again, I DO however want the same function to be used for the other searches if they still have results. Obviously once all player searches have no result i would ideally like to break the while loop too.

3
  • 1
    if you are in a function why don't you just return? Otherwise I think "break" exists in php Commented Jan 24, 2013 at 23:37
  • or 'if(player_search($f, $s, $t)!==false){.....}else{break;}' Commented Jan 24, 2013 at 23:40
  • Can you explain what exactly you are trying to do? I don't see the point of your while loop. Commented Jan 24, 2013 at 23:47

4 Answers 4

3

Use break 2; to exit both the foreach and the while

while($attempts < $cycles) {
    foreach($player_array as $player) {
        if(isset($player['results']) && $player['results'] == 1) {
            $player['results'] = player_search($f, $s, $t);
            break 2;
        }
    }
}

Example from the PHP Manual : http://php.net/manual/en/control-structures.break.php

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

Comments

2

I'm not sure whats going on with $attempts and $cycles since they dont seem to be changed from within the loop but if you really want to change the value in $player contained in $player_array you should do

foreach($player_array as &$player) {
    if(!isset($player['results'])) {
        $player['results'] = player_search($f, $s, $t);
    }
}

notice I changed $player to &$player so that it becomes a reference instead of a copy

I also changed your isset so that it only does a search if the results are not stored in results already

4 Comments

Can you explain what the &$player change means?
it makes $player a reference instead of a copy. So that when you edit it it sticks
If thats the case, you my friend seem to be the man who understands! So I return false in the function if there's no results, and i don't return anything if there is results? I need to repeat searches because I can only search a small number of items at a time. It is also a timely search so I don;t want the function to even call if there was no results on the previous search
the altered if statement I have in my answer should cover that case when it returns false.
2

Use break after you found (or not?) a match.

And to stop the while loop as well: break 2;

If this is code is within function and you have nothing else there, you might as well return the result.

Comments

2

The reason it doesnt set the variable is because you are only setting one $player with the results as 1 or 0 (or true/false). The next time in the loop, the $player variable is not the same as the previous. Why not do something like this?

while($attempts < $cycles) {
  foreach($player_array as $player) {
    if(isset($player['results']) && $player['results'] == 1) {
      if(!player_search($f, $s, $t)) break;
      else //something here
    }
  }
}

If you want to break out of the while loop also, you can always do break 2;

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.