Php 递归函数不返回所有子函数
我有一个数据库,里面有无数的问题和儿童问题。所以你可以问“你接受什么样的付款方案”。然后孩子的回答可能是“抄送处理”和“检查”。然后“抄送处理”可能会有孩子,“你们接受visa和万事达卡吗?”。等等 === 我在Laravel 5.1中设置了一个php函数和递归函数,以获取我需要的信息并创建一个“树”数组: 我的“树”数组正在返回以下内容:Php 递归函数不返回所有子函数,php,laravel,recursion,Php,Laravel,Recursion,我有一个数据库,里面有无数的问题和儿童问题。所以你可以问“你接受什么样的付款方案”。然后孩子的回答可能是“抄送处理”和“检查”。然后“抄送处理”可能会有孩子,“你们接受visa和万事达卡吗?”。等等 === 我在Laravel 5.1中设置了一个php函数和递归函数,以获取我需要的信息并创建一个“树”数组: 我的“树”数组正在返回以下内容: [ { "id": 18, "question": "How Do you Accept Payments?", "parentId": 0, "
[
{
"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
}
}
]
它失去了一些孩子。我感觉我就快到了,但我就是不能把最后的部分组合起来
根据我上面的数据库截图,我应该得到一个“你如何接受付款,id为18”的家长。那么我的子数组实际上应该是:“卡站”、“手动”和“测试”。那么“卡站”应该有孩子,“什么样的卡站”
如果您对此处缺少的链接有任何想法,我们将不胜感激。给您-在您的
getChildren
方法中,您返回了循环中的第一个“child\u分支”,它基本上被跳过了
您可以在我的第一次编辑中看到,我构建了一个$branch
数组并返回它。在以后的编辑中,我实际上是获取输入数组,在循环中修改它,并在最后返回它。我能够在查询中使用select
方法调用将输入数组限制为所需的格式
我冒昧地将“trunk”变成另一个分支(重用getChildren),从而稍微干涸了您的代码:
这是打字错误吗<代码>$brand['industryId']=$top['industryId']在哪里有
$brand
而不是$branch
?您在递归循环中查询数据库?对我来说这真是个坏主意。你考虑过使用嵌套集模型吗?这是我在Laravel上的第一个项目。不确定这是PHP的事情还是什么。你能想出什么例子吗?我已经编程多年了,信不信由你,这是我第一次尝试做这种递归。哈哈。每次迭代你都会覆盖你的$child\u分支['children']
,而且你的返回在你的foreach中,这就是为什么你只得到第一个结果。移动返回并使用$child\u branch['children'][]=…
将其添加到数组中。您忘记返回$branch
Derp。完成。:)谢谢绝对漂亮。正是我需要的。我很感激!任何使用Laravel的人在将来发现这个问题时,当他在create方法中调用函数getChildren时,您必须使用$this->getChildren($trunk);再次感谢!请注意我对你问题的评论。。。您确实不希望在递归函数中执行查询。当你的调查问卷越来越大时,你可以运行100个查询,而不是1个(性能杀手)。改为查看嵌套的集合模型。@DanielWhite—最后一点调整—使代码更加干涸。除了修复您的问题之外,希望它能解释一些PHP的最佳实践。
[
{
"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
}
}
]
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;
}