Codeigniter将数组返回到调用函数



我有一个模型,它有一个正常函数和一个递归函数,如下所示:

public function fxn1($mid)
{
//Some CRUD code is also written to fetch something for some purpose, so using fxn1 is necessary before calling the recursive function.
$arr=array();
$i=0;
return $this->fxn2($mid, $arr, $i); //THIS RETURNS NULL TO THE CONTROLLER WHILE IT SHOULD SEND THE ARRAY VALUES. 
//var_dump($this->fxn2($mid, $arr, $i)) also prints NULL here
}
public function fxn2($mid, $arr, $arrcnt)
{
//Some of my code fetches values from table and pushes them to the array
array_push($arr, valuefetchedfromtable);
$arrcnt++;
if($arrcnt >= count($arr))
{
return $arr; // THIS LINE RETURNS NULL TO FXN1
/* Using var_dump($arr); here prints the array values fine: 
array(81) { [0]=> string(8) "20181006" [1]=> string(8) "20181007" [2]=> string(8) "20181011" [3]=> string(8)…………………...and so on
*/
}
else
$this->fxn2($arr[$arrcnt-1], $arr, $arrcnt);
}

如何将$arr从fxn2返回到fxn1,以便fxn1可以将数组值返回到控制器?

因为我没有确切的代码要测试,所以我做了一些调整。请尝试用以下代码替换您的代码。

public function fxn1($mid)
{
//Some CRUD code is also written to fetch something for some purpose, so using fxn1 is necessary before calling the recursive function.
$arr=array();
$i=0;
return $this->fxn2($mid,$arr,$i); //THIS RETURNS NULL TO THE CONTROLLER WHILE IT SHOULD SEND THE ARRAY VALUES. 
//var_dump($this->fxn2($mid,$arr,$i)) also prints NULL here
}
public function fxn2($mid,&$arr,&$arrcnt) //change over here
{
//Some of my code fetches values from table and pushes them to the array
array_push($arr, valuefetchedfromtable);
$arrcnt++;
if($arrcnt>=count($arr))
{
return $arr; // THIS LINE RETURNS NULL TO FXN1
/* Using var_dump($arr); here prints the array values fine: 
array(81) { [0]=> string(8) "20181006" [1]=> string(8) "20181007" [2]=> string(8) "20181011" [3]=> string(8)…………………...and so on
*/
}
else
$this->fxn2($arr[$arrcnt-1],$arr,$arrcnt);
}

相关内容

  • 没有找到相关文章

最新更新