我有方法
private function test($testArray = array(),$i=0){
$i++;
$testArray = array_merge($testArray, array($i));
if($i < 10){
$this->test($testArray, $i); //recursion
}else{
return $testArray;
}
}
当我像$x = $this->test(array(), 0);
从另一种方法一样调用它时,假设创建一个 $i 数组,然后在 $i 达到 10 时返回该数组。问题是它返回 null 有趣的是,当我执行 var_dump 时,我得到了数组就在返回之前。这是我在调试代码中的内容
private function test($testArray = array(),$i=0){
$i++;
$testArray = array_merge($testArray, array($i));
var_dump($i);
if($i < 10){
var_dump("Continued to $i");
$this->test($testArray, $i);
}else{
var_dump("Closing on $i");
var_dump($testArray);
return $testArray;
}
}
这是输出
int 1
string 'Continued to 1' (length=14)
int 2
string 'Continued to 2' (length=14)
int 3
string 'Continued to 3' (length=14)
int 4
string 'Continued to 4' (length=14)
int 5
string 'Continued to 5' (length=14)
int 6
string 'Continued to 6' (length=14)
int 7
string 'Continued to 7' (length=14)
int 8
string 'Continued to 8' (length=14)
int 9
string 'Continued to 9' (length=14)
int 10
string 'Closing on 10' (length=13)
array (size=10)
0 => int 1
1 => int 2
2 => int 3
3 => int 4
4 => int 5
5 => int 6
6 => int 7
7 => int 8
8 => int 9
9 => int 10
null
这要了我的命,任何帮助将不胜感激