1

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.)

0

3 Answers 3

5

foreach loops through an array by taking a copy of the array and not by reference. You need to loop through the array by reference using the ampersand & on the array value.

foreach($arr as $k => &$v) {
   echo "<br> The ".$v['type'].' fruit is '.$v['color'];

   // change the color of the next fruit?
   if($v['type'] == 'banana') { $arr[$k+1]['color'] = 'green'; }
}
Sign up to request clarification or add additional context in comments.

4 Comments

That's only one part of the solution (but nevertheless very important one). When $v['type'] == 'banana' is true, the next fruit which is modified is grape. If the banana should be green it must be $v['type'] == 'apple'.
I misread, I assumed when the original poster was refering to the next element he meant to change the color of the grape to green.
Well, I don't know. Using the reference solves the programming error. But he also stated I'd like this to tell me the banana is green, in which case he also has a logic error. But your answer should get him to the right track.
Oops, my bad on the logic (I wrote that script quickly here on StackOverflow without actually testing it)... but you understood what I was after and your answer is working for me, so... thanks!
0
$k=array_keys($yourarray);
    for($i=0; $i<sizeof ($k); $i++) {
       if($yourarray[$k[$i]] == "something") {
           $yourarray[$k[$i+1]] = "something else"; 
       }
    }
}     

sorry formatting is all wonky as i am replying from a phone...

Comments

-1

The counter has to remain the same

$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'] == 'banana') { $arr[$k]['color'] = 'green'; }

  // now echo the new color from the original array 
   echo "<br> The ".$arr[$k]['type'].' fruit is now '.$arr[$k]['color'];
}

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.