<?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);
- Output for 5.6.38, 7.0.0 - 7.0.33, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
- array(3) {
[0]=>
string(1) "C"
[1]=>
string(1) "D"
[2]=>
string(1) "E"
}
preferences:
155.17 ms | 404 KiB | 219 Q