这是一个函数,它将递归地对 n 长度的数组执行“不对称”切片:
function slice_asym(array $head, $exclude, $include)
{
$keep = array_slice($head, $exclude, $include);
$tail = array_slice($head, $exclude + $include);
return $tail ? array_merge($keep, slice_asym($tail, $exclude, $include))
: $keep;
}
递归需要一个基本情况,其中递归停止并且所有范围都展开并返回。在我们的例子中,我们希望在 tail 不再包含元素时停止。
array_slice() 总是返回一个数组 - 如果没有元素返回给定的偏移量,array_slice() 返回一个空数组。因此,对于每次递归,我们都希望:
切出我们想要的元素$keep
创建一个$tail - 这是数组的一个子集,它排除了我们想要忽略并保留在当前范围内的元素 ($exclude + $include)。
如果$tail 不为空,则创建新的递归级别。否则停止递归并将所有当前的$keep 元素与下一级递归的结果合并。
在伪代码中,这看起来像:
# step 1: first recursion
head = [1,2,3,4,5,6,7,8,9,10,11,12,13]
keep = [3,4,5]
tail = [6,7,8,9,10,11,12,13] # tail not empty
# step 2: second recursion
head = [6,7,8,9,10,11,12,13]
keep = [8,9,10]
tail = [11,12,13] # tail not empty
# step 3: third recursion
head = [11,12,13]
keep = [13]
tail = [] # tail empty: base case met!
# return and merge `keep` elements
[3,4,5] + [8, 9, 10] + [13] -> [3,4,5,8,9,10,13]
根据您的示例,以下调用:
$range = range(1, 13); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
$sliced = slice_asym($range, 2, 3);
print_r($sliced);
产量:
Array
(
[0] => 3
[1] => 4
[2] => 5
[3] => 8
[4] => 9
[5] => 10
[6] => 13
)
编辑
如果您使用的是 PHP 5.5 或更高版本,这对于 generator 也是一个很好的用例。
这些很有用,因为:
生成器允许您编写使用 foreach 迭代一组数据的代码,而无需在内存中构建数组,这可能会导致您超出内存限制,或者需要大量的处理时间来生成。
因此,您不必预先创建一个顺序值数组:
相反,您可以编写一个与普通函数相同的生成器函数,不同之处在于生成器不是返回一次,而是生成器可以根据需要多次生成以提供要迭代的值。
我们可以为您的用例实现一个生成器,如下所示:
/**
*
* @param int $limit
* @param int $exclude
* @param int $include
* @return int
*/
function xrange_asym($limit, $exclude, $include) {
if ($limit < 0 || $exclude < 0 || $include < 0) {
throw new UnexpectedValueException(
'`xrange_asym` does not accept negative values');
}
$seq = 1;
$min = $exclude;
$max = $exclude + $include;
while ($seq <= $limit) {
if ($seq > $min && $seq <= $max) {
yield $seq;
}
if ($seq === $max) {
$min = $exclude + $max;
$max = $exclude + $include + $max;
}
$seq = $seq + 1;
}
}
然后像这样使用它:
foreach (xrange_asym(13, 2, 3) as $number) {
echo $number . ', '; // 3, 4, 5, 8, 9, 10, 13
}
或者使用iterator_to_array() 将整个范围生成为一个数组:
print_r(iterator_to_array(xrange_asym(13, 2, 3)));
希望这会有所帮助:)