I have an indexed array.
Say I can find out that my $myarray[4] == 123;
How do I retreve one value before and one value after, i.e:
$myarray[3] == ?; $myarray[5] == ?;
err... what do you mean? Your code snippet can do what you asked.
Dahh!!! Living up to my signature!
I guess my question should've been: if I know a value, like 123 how do I find out which $myarray[?] does it correspond to?
Oh, by using [man]array_search/man
Yeah, your questions appear more complicated than they really are 🙂
Yeah, your questions appear more complicated than they really are
Welcome to my world...
:rolleyes:
So if I found out that
($myvar == $myarray[4])
can I say that my previous and next array values are:
$thisval = $myarray[4]; $prev = $thisval - 1; $next = $thisval + 1; PHP] ???????????? :D
So if I know my key, for example
$myarray[4])
how can I found what my previous and next array values are, eg.:
$thisval = $myarray[4]; $prev = $myarray[3]; $next = $myarray[5]; PHP]
if (($key = array_search($needle, $haystack)) !== false) { $thisval = $haystack[$key]; $prev = $haystack[$key - 1]; $next = $haystack[$key + 1]; }
Of course, you may want to perform boundary checks to ensure that there is a legal $prev and $next value.