0

Have been scratching my head for a few hours on this, seems like a silly issue, but just can't find a solution.

Here's my sample code:

$continueLoop = true;
$colorsArray = array("red", "white", "blue");

while($continueLoop == true) {

$arrayCount = count($colorsArray);

for ($i=0; $i < $arrayCount; $i++) {
    echo "evaluating ".$colorsArray[$i]."<br>";
    if($colorsArray[$i] == "blue") {
        array_push($colorsArray, "YELLOW");
        break;
    }
}

if(count($colorsArray) == 4) {
    $continueLoop = false;
}

}

It outputs

red

white

blue

Basically I am adding a color "YELLOW" and then it should walk through the whole array again. But it's ignoring the newly added array item.

I know it does recognize the item, because the while loop will keep running until $colorsArray has 4 items (in the beginning it has only 3).

So why is it not Echo'ing "YELLOW"?

I've tried a few different solutions, just pushing the item using $array[], using foreach, array_values etc. etc.

Thank you

Update:

If i put

if(count($colorsArray) == 10) {

Still still won't print Yellow

5
  • 1
    You specifically prevent the additional iteration with your $continueLoop = false; command... Commented Sep 16, 2016 at 14:36
  • 1
    also break in if will not show yellow index Commented Sep 16, 2016 at 14:44
  • 1
    You break out of the for and then set $continueLoop = false so everything is done and it doesn't loop again to echo yellow. Commented Sep 16, 2016 at 14:48
  • but if I put if(count($colorsArray) == 10) it stil won't print Yellow. It will print 'red','white','blue' 8 times, but never Yellow. Is it because of the break statement? It's as if "break" will always take precedence over "echo"? Commented Sep 17, 2016 at 1:29
  • damn it, you're right.. It never reaches the new array items it adds because it breaks out of the loop when it reaches "blue" Commented Sep 17, 2016 at 15:26

1 Answer 1

1

Will probably clean this up in a moment but just quickly this should do the job;

$colorsArray = array("red", "white", "blue");

for ($i=0; $i < count($colorsArray); $i++) {
    echo "evaluating ".$colorsArray[$i]."<br>";
    if($colorsArray[$i] == "blue" && !in_array('YELLOW', $colorsArray)) {
        array_push($colorsArray, "YELLOW");
    }
}

print_r($colorsArray);

Essentially you use count on each iteration, since the length of the array changes, it now has another element to loop through.

Edit: If you want it to walk through the whole array again, just set $i = -1; after you push in a new element.

Edit2: A little clean up.

$colours = array('red', 'white', 'blue');

for ($i = 0; $i < count($colours); ++$i) {
    echo 'Evaluating: ' . $colours[$i] . '<br/>';

    if ($colours[$i] === 'blue' && !in_array('Yellow', $colours)) {
        array_push($colours, 'Yellow');
        $i = -1;
    }
}

Output:

Evaluating: red
Evaluating: white
Evaluating: blue
Evaluating: red
Evaluating: white
Evaluating: blue
Evaluating: Yellow
  • If you reset the loot to 0 rather than -1 then you skip the first loop iteration, i.e. you miss out red.
  • Not sure on your dataset / purpose for this but I imagine you would want 'Yellow' to be dynamic.
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Pigeon, appreciate the effort you put into this. The thing is that I need break statements in there. It's part of a much larger script. When I run your script it works, but it won't work with break statements inside.
nm it was a dumb question, of course it won't show yellow, because each time it breaks out of the loop when it reaches "blue".. even if i was to add 1000 yellow in there it would never show them.

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.