递归函数结果为数组未显示所需结果


Recursive function result as array not showing desired result

我有一个控制器ajax其中存在两个函数:

  function customer_comission()
  {
            ---------------------------
            ---------------------------
        $arr = $this->show_parent_id( $child_node );
        var_dump($arr);
            ----------------------------
            ---------------
  }
function show_parent_id( $cust_id ){
    if( $cust_id > 2 ):
        $cust_id2 = $this->comission_model->show_parent_id( $cust_id );
        $cust_array[] = $cust_id2;
        //echo $this->show_parent_id( $cust_id2 ); 
         $this->show_parent_id( $cust_id2 );  
    endif;
    return $cust_array; // <-- This is Line 38
}

因此,我要显示的是$cust_id层次结构的parent_id数组。echo $this->show_parent_id( $cust_id2 );打印所需的结果,但是当我尝试将它们添加到数组中时,然后出现错误显示:

遇到 PHP 错误

严重性:通知

消息:未定义的变量:cust_array

文件名:控制器/ajax.php

行号:38

这是因为每当$cust_id <= 2$cust_array变量都未定义。所以只需初始化它作为这样的 if 条件

function show_parent_id( $cust_id ){
    $cust_array = array();
    if( $cust_id > 2 ){
        $cust_id2 = $this->comission_model->show_parent_id( $cust_id );
        $cust_array[] = $cust_id2;
        //echo $this->show_parent_id( $cust_id2 ); 
         $this->show_parent_id( $cust_id2 );  
    }
    return $cust_array; // <-- This is Line 38
}