Get previous and next 5 items in an array...

I have 2 php functions:
function first(&$array) {
    if (!is_array($array)) return $array;
    if (!count($array)) return null;
    reset($array);
    return $array[key($array)];
}

function last(&$array) {
    if (!is_array($array)) return $array;
    if (!count($array)) return null;
    end($array);
    return $array[key($array)];
}

that takes an array like this:

(
    [0] => 89755
    [1] => 89753
    [2] => 89752
    [3] => 89749
    [4] => 89751
    [5] => 89756
    [6] => 89762
    [7] => 89763
    [8] => 89761
    [9] => 89799
    [10] => 89760
    [11] => 89758
    [12] => 89802
    [13] => 89803
    [14] => 135083
    [15] => 89808
    [16] => 89809
)

and I pass it an id, like '89758' and it returns the previous and next element in the array. How can I change the functions so that they return the previous and next 5 elements instead of one.
interclubsAsked:
Who is Participating?
 
Mohamed AbowardaConnect With a Mentor Software EngineerCommented:

function getPreviousAndNext5($arr, $item)
{
	$newArray = array();
	$key = array_search($item, $arr);
	if ($key === false)
		return null;

	for ($i = $key - 5; $i <= $key + 5; $i++)
	{
		if (isset($arr[$i]) && $arr[$i] != $item)
			array_push($newArray, $arr[$i]);
	}
	return $newArray;
}

Open in new window

0
 
Mohamed AbowardaSoftware EngineerCommented:
I have made the following function for you:
 
function getPreviousAndNext5($arr, $item)
{
	$newArray = array();
	$key = array_search($item, $arr);
	if ($key === false)
		return null;
	if (isset($arr[$key - 1]))
		array_push($newArray, $arr[$key - 1]);
	
	for ($i = $key; $i < $key + 5; $i++)
	{
		if (!isset($arr[$i]))
			break;
		array_push($newArray, $arr[$i]);
	}
	return $newArray;
}

Open in new window


How to use:
 
$arr = array(
		0 => 89755,
		1 => 89753,
		2 => 89752,
		3 => 89749,
		4 => 89751,
		5 => 89756,
		6 => 89762,
		7 => 89763,
		8 => 89761,
		9 => 89799,
		10 => 89760,
		11 => 89758,
		12 => 89802,
		13 => 89803,
		14 => 135083,
		15 => 89808,
		16 => 89809,
		);
print_r(getPreviousAndNext5($arr, "89761"));

Open in new window

0
 
interclubsAuthor Commented:
Hmmm, that seems to work perfectly for returning the next 5, how would I change it to return the previous?

Thanks!
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
Mohamed AbowardaSoftware EngineerCommented:
Do you want to return all the previous items or only one?
0
 
interclubsAuthor Commented:
the 5 previous items and the 5 next items. Right now I use 2 functions which is fine, if that makes it easier.
0
 
interclubsAuthor Commented:
Genius!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.