I have an array:
$colors = array("red", "green");
I am using this array in foreach
and I want to update this array inner the foreach
for example like this:
foreach( $colors as $color ){
if( $color=='green' ){
array_push($colors, 'blue'); //now $colors is ["red", "green", "blue"]
}
echo $color . "<br>";
}
Result is:
red
green
and blue
is not echo in result!
How I can update foreach
variable inner it?
update:
I do this with for
and it is work.
$colors = array("red", "green");
for( $i=0; $i < count($colors); $i++ ){
if( $colors[$i]=='green' ){
array_push($colors, 'blue'); //now $colors is ["red", "green", "blue"]
}
echo $colors[$i]."<br>";
}
result is
red
green
blue
How I can do this with foreach
?