How can I modify the next element in a foreach() loop, during the loop? I think it has something to do with talking to the variable by reference but I'm not sure how. i.e.:
$arr = array( array('color' => 'red', 'type' => 'apple'),
array('color' => 'yellow', 'type' => 'banana'),
array('color' => 'purple', 'type' => 'grape')
);
foreach($arr as $k => $v) {
echo "<br> The ".$v['type'].' fruit is '.$v['color'];
// change the color of the next fruit?
if($v['type'] == 'apple') { $arr[$k+1]['color'] = 'green'; }
}
I'd like this to tell me the banana is green, but it stubbornly sticks to the banana being yellow...
(UPDATE: fixed a stupid logic error in my original question. The answer marked below is correct.)