I have this
array([0] => 4, [1] => 6, [2] => 8, [3] =>11);
$value = 6;//(refers to [1])
After the core code, my array COULD change becoming for example:
array([0] => 4, [1] => 8, [2] =>11);
I'd like to have a variable ($newvalue
) set to the next value if the $value
key has been removed ($newvalue=8
), or stay the same ($newvalue=6
) if 6
is still in the values of that array.
NB if $value
is the last (11), and 11 has been removed, $newvalue
should be set to 4.
To summarize:
$value = 11; /* array([0] => 4, [1] => 6, [2] => 8); */ $newvalue = 4;
$value = 11; /* array([0] => 4, [1] => 6, [2] => 8, [3] => 11); */ $newvalue = 11;
Assuming that your array values are always in ascending order:
if (in_array($value, $the_array)) {
$newvalue = $value;
} else if ($value > max($the_array)) {
$newvalue = $the_array[0];
} else {
foreach ($the_array as $v) {
if ($v > $value) {
$newvalue = $v;
break;
}
}
}