<?php $collection = [ 'A', 'B', 'C', 'D', 'E' ]; $offset = 4; $limit = 2; If($offset>count($collection) || $offset < 0 || $limit ==0) die("inputs out of bounds"); If($offset-$limit >=0){ $start = $offset-$limit; }else{ $start =0; } If($offset+$limit>count($collection)){ $end = count($collection)-$offset+$limit; }else{ $end = $offset + $limit; } If($start ==0) $end++; $result = array_slice($collection, $start, $end); Var_dump($result);
You have javascript disabled. You will not be able to edit any code.