递归函数不返回所有子项

时间:2015-10-29 02:09:53

标签: php laravel recursion

我设置了一个包含大量问题和子问题的数据库。因此,您可以提出类似的问题,"您接受哪种付款解决方案"。那么孩子的答案可能是," CC Processing"和"检查"。然后" CC处理"可能有孩子,"你接受签证和万事达卡"。等

enter image description here

===

我在Laravel 5.1中设置了一个php函数和递归函数来获取我需要的信息并创建一个"树"阵列:

public function create($id)
{
    //BUILD THE TREE
    $tree = array();
    //GET TOP LEVEL ITEMS
    $top_level = Questions::where('parentId', 0)->where('industryId', $id)->get()->toArray();

    foreach($top_level as $top){

        $branch = array();
        $branch['id'] = $top['id'];
        $branch['industryId'] = $top['industryId'];
        $branch['question'] = $top['question'];
        $branch['parentId'] = $top['parentId'];
        $branch['endPoint'] = $top['endPoint'];
        $branch['order'] = $top['order'];

        $children = Questions::where('parentId', $top['id'])->get()->toArray();

        //CHECK FOR CHILDREN
        if(count($children > 0)) {

            //THERE ARE CHILDREN PASS THEM TO A RECURSIVE FUNCTION TO GET LIST
            $branch['children'] = $this->getChildren($children);

        }else {
            //THERE ARE NO CHILDREN SET TO EMPTY ARRAY
            $branch['children'] = array();
        }

        $tree[] = $branch;

    }

    //Send the question tree to the view...
    $data['questions'] = $tree;

    //Grab the industry
    $data['industry'] = Industries::where('id', $id)->get();

    return $data['questions'];
    //return view('questions.create', $data);
}

public function getChildren($children) {

    foreach($children as $child){
        $child_branch = array();
        $child_branch['id'] = $child['id'];
        $child_branch['industryId'] = $child['industryId'];
        $child_branch['question'] = $child['question'];
        $child_branch['parentId'] = $child['parentId'];
        $child_branch['endPoint'] = $child['endPoint'];
        $child_branch['order'] = $child['order'];

        $children = Questions::where('parentId', $child['id'])->get()->toArray();
        //CHECK FOR CHILDREN
        if(count($children > 0)) {
            //THERE ARE CHILDREN PASS THEM TO THIS FUNCTION (RECURSION) TO GET LIST
            $child_branch['children'] = $this->getChildren($children);
        }else {
            //THERE ARE NO CHILDREN SET TO EMPTY ARRAY
            $child_branch['children'] = array();
        }

        return $child_branch;
    }
}

我的"树"数组返回以下内容:

    [
{
"id": 18,
"question": "How Do you Accept Payments?",
"parentId": 0,
"endPoint": 0,
"order": 0,
"children": {
"id": 19,
"industryId": 1,
"question": "Card Station",
"parentId": 18,
"endPoint": 0,
"order": 0,
"children": {
"id": 25,
"industryId": 1,
"question": "What type of card station?",
"parentId": 19,
"endPoint": 0,
"order": 0,
"children": null
}
}
},
{
"id": 21,
"question": "What?",
"parentId": 0,
"endPoint": 0,
"order": 0,
"children": {
"id": 22,
"industryId": 1,
"question": "Testing This",
"parentId": 21,
"endPoint": 0,
"order": 0,
"children": null
}
}
]

缺少一些孩子。我觉得我已经在那里了,但我不能把最后的作品放在一起。

根据我上面的数据库截图,我应该得到一个父母#34;你如何接受付款,ID为18"。然后我的子阵列应该实际上是:"卡站","手动"和"测试"。然后"卡站"应该有孩子,"什么类型的卡片站"。

对此处缺失链接的任何想法表示赞赏。

1 个答案:

答案 0 :(得分:2)

在这里 - 你的getChildren方法,你在循环中返回第一个“child_branch”,基本上跳过了。

你可以在我的第一次编辑中看到,我正在构建一个$branch数组并返回它。在后来的编辑中,我实际上是在接受输入数组,在循环中改变它,并在最后返回它。我可以使用查询中的select方法调用将输入数组限制为所需的格式。

我冒昧地干掉你的代码,让“trunk”只是另一个分支(重用getChildren):

public function getQuestions($parentId, $industryId=null)
{
    $query = Questions::where('parentId', $parentId);
    if ($industry) {
        $query->where('industryId', $id);
    }
    return $query->select('id', 'industryId', 'question', 'parentId', 'endPoint', 'order')->get()->toArray();
}

public function create($id)
{
    //BUILD THE TREE
    $tree = array();
    //GET TOP LEVEL ITEMS
    $trunk = $this->getQuestions(0, $id);
    $tree = $this->getBranch($trunk);

    //Send the question tree to the view...
    $data['questions'] = $tree;

    //Grab the industry
    $data['industry'] = Industries::where('id', $id)->get();

    return $data['questions'];
    //return view('questions.create', $data);
}

public function getBranch($twigs) {
    foreach ($twigs as $twig) {
        // Check for children
        $children = $this->getQuestions($twig['id']);
        $twig['children'] = count($children) ? $this->getBranch($children) : array();
    }
    return $twigs;
}