Codeigniter将数组返回到调用函数

时间:2018-11-30 13:26:11

标签: php arrays function 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可以将数组值返回给控制器?

1 个答案:

答案 0 :(得分:0)

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

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);
}