PHP递归级别

发布于 2024-09-24 07:44:34 字数 970 浏览 3 评论 0原文

我有递归函数。有一个层次结构的用户结构。我将用户 ID 发送到我的函数,它应该找到此下的所有用户。函数返回所有关联用户的数组。我的任务是找到该用户的级别。

例如:

        User1
       /    \
    User2   User3
   /    \    \ 
User4 User5  User6

用户1的级别为0。 User2、User3 的级别为 1。 User4、User5、User6 的级别为 2。 我怎样才能在递归中找到这个? 这是我的代码:

private function getAssociates($userId) {
    global $generation;
    global $usersUnder;
    if (!isset($generation)) {
        $generation = 1;
    }
    $userDb           =  new Lyf_DB_Table('user');
    $associatesSelect =  $userDb->Select();
    $associatesSelect -> from('user', array('id'))->where('enroller_id = ?', $userId);
    $associates       =  $userDb->fetchAll($associatesSelect)->toArray();
    if (!empty($associates)) {
        foreach ($associates as $associate) {
            $usersUnder[$generation] = $associate['id'];
            $this->getAssociates($associate['id']);
        }
    }
    return $usersUnder;
}

I have the recursion function. There are an hierarchy users structure. I send a user id to my function and it should find all user under this. Function returns an array of all associates users. My task is to find a levels of this users.

For example:

        User1
       /    \
    User2   User3
   /    \    \ 
User4 User5  User6

User1 have level 0.
User2, User3 have level 1.
User4, User5, User6 have level 2.
How can I find this in my recursion?
It is my code:

private function getAssociates($userId) {
    global $generation;
    global $usersUnder;
    if (!isset($generation)) {
        $generation = 1;
    }
    $userDb           =  new Lyf_DB_Table('user');
    $associatesSelect =  $userDb->Select();
    $associatesSelect -> from('user', array('id'))->where('enroller_id = ?', $userId);
    $associates       =  $userDb->fetchAll($associatesSelect)->toArray();
    if (!empty($associates)) {
        foreach ($associates as $associate) {
            $usersUnder[$generation] = $associate['id'];
            $this->getAssociates($associate['id']);
        }
    }
    return $usersUnder;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青巷忧颜 2024-10-01 07:44:34

向您的 getAssociates() 函数添加一个额外的参数:

private function getAssociates($userID, $level = 0) {

当您处理树的该级别时,将 $level 与其余用户数据一起存储,然后递归到函数中:

$this->getAssociates($associate['id'], $level + 1);

当您最初调用该函数来启动此过程时,为 $level 传入 0,或者将其留空并让 PHP 分配默认值 (也是0)。

Add an extra parameter to your getAssociates() function:

private function getAssociates($userID, $level = 0) {

and when you're processing that level of the tree, store the $level with the rest of the user data, then recurse into the function with:

$this->getAssociates($associate['id'], $level + 1);

and when you initially call the function to start this process, pass in 0 for $level, or leave it blank and let PHP assign the default (also 0).

夜夜流光相皎洁 2024-10-01 07:44:34

看一下迭代器:

$user_array= array('1',array(array('2')));
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($user_array));
foreach($it as $user){

     print_r($it->getDepth());
}

Have a look at iterators:

$user_array= array('1',array(array('2')));
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($user_array));
foreach($it as $user){

     print_r($it->getDepth());
}
蒗幽 2024-10-01 07:44:34

简单,但我不在 zend 中工作,所以我不能告诉你代码,但我可以给你描述

make 函数

function getLevel($id,$level=0){
   take from db(higher lever higher_id if exist){
     $level++
     $level = getLevel(higher_id,$level);
   }
   return $level;    
}

,然后它们调用

$level = getLevel($id);

easy but I don't work in zend so I can't tell you code but I can give you description

make function

function getLevel($id,$level=0){
   take from db(higher lever higher_id if exist){
     $level++
     $level = getLevel(higher_id,$level);
   }
   return $level;    
}

and them call

$level = getLevel($id);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文