首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Laravel -层次递归

Laravel -层次递归
EN

Stack Overflow用户
提问于 2021-06-24 14:34:07
回答 2查看 61关注 0票数 0

我有一个marketer表,每个营销商可以有多个营销商,也可以属于一个营销商。我写这个方法是为了检查我是否可以得到一个特定营销人员的多个级别:

表:

代码语言:javascript
运行
复制
id   parent_id    name
1    NULL         a
2    1            b
3    2            c
4    1            d
5    3            e
6    2            f
7    4            g

营销者模型:

代码语言:javascript
运行
复制
public function parent()
{
    return $this->belongsTo(Self::class);
}

public function children()
{
    return $this->hasMany(Self::class,'parent_id');
}

方法:

代码语言:javascript
运行
复制
public function marketerWithSubs(Marketer $marketer, $index = 1, $output = [])
{
    $output = [];
    foreach ($marketer->children as $firstLevel){
        $output[1][] = $firstLevel->id . '- ' . $firstLevel->full_name;
        foreach ($firstLevel->children as $secondLevel){
            $output[2][] = $secondLevel->id . '- ' . $secondLevel->full_name;
            foreach ($secondLevel->children as $thirdLevel) {
                $output[3][] = $thirdLevel->id . '- ' . $thirdLevel->full_name;
                foreach ($thirdLevel->children as $forthLevel) {
                    $output[4][] = $forthLevel->id . '- ' . $forthLevel->full_name;
                    foreach ($forthLevel->children as $fifthLevel) {
                        $output[5][] = $fifthLevel->id . '- ' . $fifthLevel->full_name;
                        foreach ($firstLevel->children as $sixthLevel) {
                            $output[6][] = $sixthLevel->id . '- ' . $sixthLevel->full_name;
                        }
                    }
                }
            }
        }
    }

    return $output;
}

方法示例输出,这是我所期望的,数组中的索引显示了(所谓的) submarketer的级别,值是一个数组形式的submarketers:

代码语言:javascript
运行
复制
array:6 [▼
  1 => array:27 [▶]
  2 => array:23 [▶]
  3 => array:32 [▶]
  4 => array:8 [▶]
  5 => array:5 [▼
    0 => 102
    1 => 63
    2 => 64
    3 => 65
    4 => 67
  ]
  6 => array:35 [▶]
]

如何将其转换为实用的方法?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-06-24 17:53:03

好吧,我想我找到答案了:

代码语言:javascript
运行
复制
public function getMarketerWithSubs3(Marketer $marketer, $index = 1, $output = [])
{
    foreach ($marketer->children as $firstLevel){
        $output[$index][] = $firstLevel->id;
        $output = $this->getMarketerWithSubs3($firstLevel, $index+1, $output);
    }

    return $output;
}
票数 0
EN

Stack Overflow用户

发布于 2021-06-24 15:22:22

$output可能必须通过引用传递才能正常工作

代码语言:javascript
运行
复制
public function marketerWithSubs(Marketer $marketer, $index = 1, &$output = [])
{
    return array_reduce($marketer->children->all(), function($output, $child) {
        $output[$index][] = $child->id . '- ' . $child->full_name;
        if ($child->children->count()) {
            $this->marketerWithSubs($child, $index++, $output);
        }
        return $output;
    }, $output);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68110652

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档