Skip to content

Instantly share code, notes, and snippets.

@pietvanzoen
Last active December 20, 2015 07:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pietvanzoen/6097137 to your computer and use it in GitHub Desktop.
Save pietvanzoen/6097137 to your computer and use it in GitHub Desktop.
Find Next Array Item :: This function will find the next item in an array. Useful for dynamically generating a nav link for the next sibling page based on the $nav array. #fuel
<?php
function find_next($array, $search, $key, $loop = false)
{
// $array = array to search
// $search = string to search against, e.g. current uri_string()
// $key = array key to compare $search against
// $loop = if set to TRUE will return first value if it reaches the end of the array
$current = false;
$next = '';
foreach ($array as $data)
{
if ($current === true)
{
$next = $data;
break;
}
if ($search === $data[$key])
{
$current = true;
}
}
if (!empty($next))
{
return $next;
}
else
{
if (!$loop) return FALSE;
return reset($array);
}
}
// USAGE
$nav = fuel_nav(array('render_type' => 'array', 'parent' => uri_segment(1)));
$next = findnext($nav, uri_string(), 'location');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment