I have an array with of enumerating numbers, like:
$pageNumbers = array(1,2,3,4,5,6,7,8,9,10);
Now I have an active page number $currentPage and want to have based on this, before and after 2 elements - a total number of 5.
$currentPage = 2:
Return: array(1,2,3,4,5)
$currentPage = 6
Return: array(4,5,6,7,8)
$currentPage = 10
Return: array(6,7,8,9,10)
Unfortunately, I did not come up with an elegant and simple method to solve this (2x while, 1 big foreach and so on). Maybe you have an idea.
My first idea was:
foreach ($pageNumbers as $page) {
if($page < $currentPage+3 && $page > $currentPage-3) {
array_push(...);
}
}
This will work for the $currentPage = 6, but if $currentPage = 1, it will only return 1,2,3.
array_searchandarray_splicewould work fine.range($start,$end)and then a tiny bit of math and some checks for first and last page,