prev

Description

prev() behaves just like next(),
except it rewinds the internal array pointer one place instead of advancing it.

Parameters

array

The input array.

Return Values

Returns the array value in the previous place that's pointed to by
the internal array pointer, or FALSE if there are no more
elements.

Warning

This function may
return Boolean FALSE, but may also return a non-Boolean value which
evaluates to FALSE. Please read the section on Booleans for more
information. Use the ===
operator for testing the return value of this
function.

// Loop through array until match is found, then set $prev and $next to the respective keys if existwhile ( !is_null($key = key($array)) ) {$val = current($array); if($key == $value) {prev($array); // Get entry before this one$prev = key($array);next($array); // Skip current entry as this was what we were looking for nearest tonext($array); // Get entry after this one$next = key($array); break; }next($array); }

Some corrections to the previous function.- When prev() and next() distance is now calculated properly returning the 'closer' one.- When exact is false, and an item exists with the key searched for, it now returns that entry instead of next/prev closest- At ends of the array, sometimes when moving past using next() or prev(), it caused results to die. This was corrected.- Added field to result stating whether the match was 'exact' ('exact'=>true), or nearest ('exact'=>false)