从位置开始的 PHP 数组切片 + 尝试返回固定数量的项目



我正在寻找一个有效的函数来实现以下目标。假设我们有一个数组:

$a = [0, 1, 2, 3, 4, 5, 6, 7];

从一个位置切片应始终返回 5 个值。 仓位index前 2 个,仓位index后 2 个值 - 当然,仓位本身index

如果位置index位于数组的开头,即0(示例 2)中,该函数应返回接下来的 4 个值。同样,如果位置index位于数组的末尾(示例 3),则该函数应返回前 4 个值。

下面是可以传递给函数和预期结果的各种索引的一些示例:

$index = 3; // Result: 1, 2, 3, 4, 5. *example 1
$index = 0; // Result: 0, 1, 2, 3, 4. *example 2
$index = 7; // Result: 3, 4, 5, 6, 7. *example 3
$index = 6; // Result: 3, 4, 5, 6, 7. *example 4

如示例所示:(示例1、示例 4),函数应始终尝试捕获成功且位于位置index之前的令牌 - 如果可以,同时始终返回总共 5 个值。

该函数必须对较小的数组无懈可击:即,如果$a有 4 个值,而不是 5 个,则该函数应该只返回所有内容。

像这样的东西?

@edit:

对不起,我误读了您的原始要求。第二次尝试:

function get_slice_of_5($index, $a) {
if ($index+2 >= count($a)) {
return array_slice($a, -5, 5)
}
else if($index-2 <= 0) {
return array_slice($a, 0, 5)  
}
else return array_slice($a, $index-2, 5)
}

通过计算开始位置来创建起始位置,并使用内爆和数组切片返回字符串。

$a = [0, 1, 2, 3, 4, 5, 6, 7];

$index = 3; // Result: 1, 2, 3, 4, 5. *example 1
echo pages($a, $index) . "n";
function pages($a, $index){
if($index >= count($a)-2){
$start = count($a)-5; // index is at end of array
}elseif($index <=2){
$start = 0; // index is at start
}else{
$start = $index-2; // index is somewhere in the middle
}
return implode(", ", array_slice($a, $start, 5));
}

https://3v4l.org/aNZsB

这是一个"独立"函数,用于获取任何大小的拼接数组:

$a = [1,2,3,4,5,6,7,8,9];
echo "<pre>"; print_r(array_slicer($a, 2));
function array_slicer($arr, $start){
// initializations
$arr_len = count($arr);
$min_arr_len = 5; // the size of the spliced array
$previous_elements = 2; // number of elements to be selected before the $start
$next_elements = 2; // number of elements to be selected after the $start
$result = [];
// if the $start index doesn't exist in the given array, return false!
if($start<0 || $start>=$arr_len){
return false;
} elseif($arr_len <= $min_arr_len){ // if the size of the given array is less than the d size of the spliced array, return the whole array!
return $arr;
}
// check if the $start has less than ($previous_elements) before it
if($arr_len - ($arr_len - $start) < $previous_elements){
$next_elements += ($next_elements - ($arr_len - ($arr_len - $start)));
} elseif(($arr_len - 1 - $start) < $next_elements){ // check if the $start has less than ($next_elements) after it
$previous_elements += ($previous_elements - ($arr_len - 1 - $start));
}
for($i = ($start-$previous_elements); $i <= ($start + $next_elements); $i++){
if($i>-1 && $i<$arr_len){
$result[] = $arr[$i];
}
}   
return $result;
}

您可以通过利用min()max()来定义array_slice()开始的边界。 假设您的数组始终至少有 5 个元素,您可以使用:

array_slice($a, min(count($a) - 5, max(0, $index - 2)), 5)

所选索引将位于切片数组的中心,除非它不能。

动态代码:(演示)

$a = [0, 1, 2, 3, 4, 5, 6, 7];
$count = count($a);
$span = 5; // most sensible with odd numbers
$center = (int)($span / 2);
foreach ($a as $i => $v) {
printf(
"%d: %sn",
$i,
implode(
',',
array_slice(
$a,
min($count - $span, max(0, $i - $center)),
$span
)
)
);
}

输出:

0: 0,1,2,3,4
1: 0,1,2,3,4
2: 0,1,2,3,4
3: 1,2,3,4,5
4: 2,3,4,5,6
5: 3,4,5,6,7
6: 3,4,5,6,7
7: 3,4,5,6,7

最新更新