Given a simple array of 10 delicious fruits:
$fruits = [
'apple',
'kumquat',
'pomegranate',
'jujube',
'plum',
'passon_fruit',
'pineapple',
'strawberry',
'raspberry',
'grapefruit',
];
I would like to retrieve a specific range/spread, using a given key as a reference. The range should always be the exact length, so the starting point might differ.
Easiest example from the middle: key is 4 (plum) with length 5 would result in:
$result = [
'pomegranate',
'jujube',
'plum', // reference
'passon_fruit',
'pineapple',
];
However, with key 1 (kumquat), we only have item 0 left and need 4 more at the end:
$result = [
'apple',
'kumquat', // reference
'pomegranate',
'jujube',
'plum',
];
Same goes for key 8 (raspberry), which needs more items from the beginning of the array:
$result = [
'passon_fruit',
'pineapple',
'strawberry',
'raspberry', // reference
'grapefruit',
];
Edit
As suggested by @berend, here's what I tried. Please note that this does not fully do what I intended:
function getRange(array $array, int $ref, int $spread)
{
$min = $ref - round(($spread / 2), 0, PHP_ROUND_HALF_DOWN);
$max = $ref + round(($spread / 2), 0, PHP_ROUND_HALF_UP);
if ($min < 0) {
$max += -$min;
$min = 0;
}
if ($max > (count($array) - 1)) {
$min = $min - ($max - count($array) - 1);
// not enough items
if ($min < 0) {
$min = 0;
}
}
return array_intersect_key($array, range($min, $max));
}
var_dump(getRange($fruits, 4, 5));
var_dump(getRange($fruits, 1, 5));
var_dump(getRange($fruits, 8, 5));