匿名函数递归不返回指定的计数级别

时间:2014-06-07 23:17:57

标签: php function class laravel anonymous-function

我正在研究树形结构关系 - 向后。这意味着,我的数据库中的用户被其他用户引用到该站点,我需要检索最高10级的上部引用者。

我在我的函数中使用了匿名函数,但它说“达到了'100'的最大函数嵌套级别,正在中止!”我在这做错了什么?

这是我的代码:

public function showSponsors($user_id, $count = 10)
{
    //The array to store referrers
    $Referrers = array();       
    //The anonymous function that uses Laravel Eloquent model to retrieve data
    $GetReferrers   = function($id) use(&$Referrers, &$count, &$GetReferrers)
    {           
        //Get the first referrer and store id as index and name as value            
        $Referrers[$upper = User::find($id)->referrer] = User::find($upper)->Name;

        //Make sure you do not return more than $count
        //Make sure the referrer returned has a referrer to return
        if(count($Referrers) < $count || !empty($upper || $upper != $user_id)){
            $count--; return $GetReferrers($upper);

        }
        //Otherwise return the referrers;
        return $Referrers;
    };

    //Run the function using the user id
    $GetReferrers($user_id);

    //return the results stored in the Referrers array      
    return $Referrers;

1 个答案:

答案 0 :(得分:0)

我猜您应该将$ GetReferrers称为$ GetReferrers($ upper,$ count)。它无法访问嵌套调用中的原始$ count变量。