// return the key or value before whatever is in $current public function previous($array, $current, $use_key = false) { // we'll return null if $current is the first in the array (there is no previous) $previous = null;
// we'll return false if $current does not exist in the array return false; }
// return the key or value after whatever is in $current public function next($array, $current, $use_key = false) { $found = false;
foreach ($array as $key => $value) { // $current was found on the previous loop if ($found) { return $use_key ? $key : $value; } $matched = $use_key ? $key === $current : $value === $current; if ($matched) { $found = true; } }
if ($found) { // we'll return null if $current was the last one (there is no next) return null; } else { // we'll return false if $current does not exist in the array return false; } }