-2

I have following the recursive function:

public function get_referred_list_down($referral,$level){
    if($level>1){
        $users = $this->Invoice->User->find('list',array('conditions'=>array('User.referral'=>$referral),'fields'=>array('User.id','User.username')));
        $this->get_referred_list_down($users,$level-1);
    }else{
        $users = $this->Invoice->User->find('list',array('conditions'=>array('User.referral'=>$referral),'fields'=>array('User.id','User.username')));
    }
    return $users;
}

I am calling it as $this->get_referred_list_down('DSTL00004',2);

In if it returns the following array:

Array
(
    [9] => DSTL00005
    [10] => DSTL00006
    [11] => DSTL00007
    [12] => DSTL00008
    [13] => DSTL00009
)

I have passed it to the function again and it gives the following in else:

Array
(
    [14] => DSTL00010
    [15] => DSTL00011
    [16] => DSTL00012
    [17] => DSTL00013
)

But it returns the result of if rather than returning the result of else.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
عثمان غني
  • 2,786
  • 4
  • 52
  • 79

1 Answers1

0

It was solved by adding a return before the recursive call to the function, so now the code looks like:

public function get_referred_list_down($referral, $level){
    if($level>1){
        $users = $this->Invoice->User->find('list', array('conditions' => array('User.referral' => $referral), 'fields' => array('User.id', 'User.username')));
        return $this->get_referred_list_down($users, $level - 1);
    }else{
        $users = $this->Invoice->User->find('list', array('conditions' => array('User.referral' => $referral), 'fields' => array('User.id', 'User.username')));
    }
    return $users;
}

Thanks to PHP Recursive function not returning value.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
عثمان غني
  • 2,786
  • 4
  • 52
  • 79