PHP array get next key/value in foreach()
arrays, foreach, loops, php
Solution
You can't access that way the next and next-next values.
But you can do something similar:
$a = array('leg1'=>'LA', 'leg2'=>'NY', 'leg3'=>'NY', 'leg4'=>'FL');
$keys = array_keys($a);
foreach(array_keys($keys) AS $k ){
$this_value = $a[$keys[$k]];
$nextval = $a[$keys[$k+1]];
$nextnextval = $a[$keys[$k+2]];
if($nextval == $this_value && $nextnextval == $this_value){
//staying put for next two legs
}
}
Problem
I am looking for a way to get the next and next+1 key/value pair in a foreach(). For example: ``` $a = array('leg1'=>'LA', 'leg2'=>'NY', 'leg3'=>'NY', 'leg4'=>'FL'); foreach($a AS $k => $v){ if($nextval == $v && $nextnextval == $v){ //staying put for next two legs } } ```