Next and Previous Element in Php Array


November 12, 2018 2 minutes php functions , php array

getting next element from array

function get_next_element ($needle, $haystack) {
  $index = array_search($needle, $haystack);
  if (count($haystack) < 2) {
    return false;
  }
  if ($index < (count($haystack) - 1)) {
    $index++;
  }else {
    $index = 0;
  }
  return $haystack[$index];
}

Previous Element

function get_prev_elemnt($needle, $haystack) {

  if (count($haystack) < 2) {
    return false;
  }
  $index = array_search($needle, $haystack);
  if ($index == 0 || $index < 1) {
    $index = count($haystack) - 1;
  }else {
    $index--;
  }
  return $haystack[$index];
}

Testing

$needle = 3;
$haystack = [3, 2, 4, 5, 6];
echo get_prev_elemnt($needle, $haystack);

See Also