• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 400
  • Last Modified:

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.
0
interclubs
Asked:
interclubs
  • 3
  • 3
1 Solution
 
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
 
Mohamed AbowardaSoftware EngineerCommented:
Do you want to return all the previous items or only one?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
Mohamed AbowardaSoftware 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
 
interclubsAuthor Commented:
Genius!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now