Php 递归函数不附加到数组

Php 递归函数不附加到数组,php,arrays,recursion,Php,Arrays,Recursion,我有一个生成日期对矩阵的函数。具体来说,它需要两个日期并将31天的范围添加到一个数组中,因此大致上,它看起来像:[[date,date+31],[date,date+31],…] 我正在使用递归函数来执行此操作: public function getBatchDates(Carbon $start, Carbon $end, array $arr) { $setEnd = Carbon::createFromTimestamp($start->getTimestamp()

我有一个生成日期对矩阵的函数。具体来说,它需要两个日期并将31天的范围添加到一个数组中,因此大致上,它看起来像:
[[date,date+31],[date,date+31],…]

我正在使用递归函数来执行此操作:

public function getBatchDates(Carbon $start, Carbon $end, array $arr) {
        $setEnd = Carbon::createFromTimestamp($start->getTimestamp())->addDays(31);
        if($setEnd->greaterThanOrEqualTo($end)) {
            $setEnd = $end;
            array_push($arr, array($start, $setEnd));
            return;
        }

        array_push($arr, array($start, $setEnd));

        $this->getBatchDates($setEnd, $end, $arr);
}
现在,当我调试调用这个函数的测试时,它似乎工作正常:

但是,测试大致如下:

$array = array();
getBatchDates(new Carbon("first day of December 2015"),Carbon::now(), $array);

$this->assertNotNull($array);
$this->assertGreaterThan(0, sizeof($array));

它失败,因为
$array
的长度为0。如果我遗漏了什么,调试器会让它看起来像是在工作。

PHP中的数组不是通过引用传递到函数中的,而是通过值传递到函数中的。您需要将函数签名更改为
公共函数getBatchDates(Carbon$start,Carbon$end,array&$arr)
(我认为),或者返回修改后的数组。

PHP中的数组不是通过引用传递到函数中的,而是通过值传递到函数中的。您需要将函数签名更改为
公共函数getBatchDates(Carbon$start、Carbon$end、array&$arr)
(我想)或返回修改后的数组