首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从分层数据生成分层URL

从分层数据生成分层URL
EN

Stack Overflow用户
提问于 2020-05-19 04:12:19
回答 1查看 32关注 0票数 0

我正在尝试实现一个基于this jQuery插件的决策树。它需要以下格式的数据:

代码语言:javascript
复制
$tree = array(
    "href" => "/",
    "label" => "Title",
    "nodes" => array(
        array(
            "href" => "/q1",
            "label" => "Question 1?",
            "nodes" => array(
                array(
                    "href" => "/q1/a1",
                    "label" => "Answer 1",
                    "nodes" => array(
                        "href" => "/q1/a1/q11",
                        "label" => "Question 1.1?",
                        "nodes" => array(
                            array(
                                "href" => "/q1/a1/q11/a11",
                                "label" => "Answer 1.1",
                                "nodes" => null
                            ),
                            array(
                                "href" => "/q1/a1/q11/a12",
                                "label" => "Answer 1.1",
                                "nodes" => null
                            )
                        )
                    )
                ),
                array(
                    "href" => "/q1/a2",
                    "label" => "Answer 2",
                    "nodes" => null
                )
            )
        )
    )
);

我从数据库中以平面列表的形式获取结果,然后能够使用递归函数生成树结构:

代码语言:javascript
复制
function buildTree(array $elements, $rootId = 0, $current_path = '', $new_path = '') {
    $ctr = 0;
    $branch = array();
    foreach($elements as $element) {
        if ($element['root_id'] == $rootId) {
            $array = array(
                'root_id' => $element['root_id'],
                'label' => $element['label'],
                'text' => $element['text'],
            );
            if(!empty($current_path)){
                $path = $current_path . ++$ctr;
                $array['href'] = $path;
            }
            $new_path = $new_path == '/q' ? '/a' : '/q';
            $children = buildTree($elements, $element['id'], $path . $new_path, $new_path);
            if ($children) {
                $array['nodes'] = $children;
            } 
            $branch[] = $array;
        }
    }
    return $branch;
}

$tree = buildTree($results);

这会产生:

代码语言:javascript
复制
$tree = array(
    "href" => "/",
    "label" => "Title",
    "nodes" => array(
        array(
            "href" => "/q1",
            "label" => "Question 1?",
            "nodes" => array(
                array(
                    "href" => "/q1/a1",
                    "label" => "Answer 1",
                    "nodes" => array(
                        "href" => "/q1/a1/q1",
                        "label" => "Question 1.1?",
                        "nodes" => array(
                            array(
                                "href" => "/q1/a1/q1/a1",
                                "label" => "Answer 1.1",
                                "nodes" => null
                            ),
                            array(
                                "href" => "/q1/a1/q1/a1",
                                "label" => "Answer 1.1",
                                "nodes" => null
                            )
                        )
                    )
                ),
                array(
                    "href" => "/q1/a2",
                    "label" => "Answer 2",
                    "nodes" => null
                )
            )
        )
    )
);

这给出了正确的'href's最大深度为2,但又偏离了预期的格式。我不知道如何获得‘href’的正确。

如何使用递归函数生成正确的'href‘,正如jQuery插件所期望的那样?

EN

回答 1

Stack Overflow用户

发布于 2020-05-19 05:42:03

代码语言:javascript
复制
$path = $current_path . ++$ctr;

即使在递归的时候,这个值对于特定的函数也是局部化的。它永远不会也似乎永远不会移动到1以上,因为在迭代一次之后,它会再次调用buildTree。

q11是否未存储在数据库中?您需要一种方法来递增/解析/收集(我不确定确切的值应该是什么),当它递归时,该值应该是什么。这个值是否应该从1.1中删除.

试着用这个函数代替你的函数,看看它有没有什么改变:

代码语言:javascript
复制
function buildTree(array $elements, $rootId = 0, $current_path = '', $new_path = '', $parent_id = 0, $ctr = 0) {
    $branch = array();
    foreach($elements as $element) {
        if ($element['root_id'] == $rootId) {
            $array = array(
                'root_id' => $element['root_id'],
                'label' => $element['label'],
                'text' => $element['text'],
            );
            if(!empty($current_path)){
                if($parent_id > 0 ) {
                    $path = $current_path . $parent_id . $ctr;
                }else{
                    $path = $current_path . ++$ctr;
                }
                $array['href'] = $path;
            }
            $new_path = $new_path == '/q' ? '/a' : '/q';
            $parent_id++;
            $children = buildTree($elements, $element['id'], $path . $new_path, $new_path, $parent_id, $ctr);
            $parent_id--;
            if ($children) {
                $array['nodes'] = $children;
            }
            $branch[] = $array;
        }
    }
    return $branch;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61878181

复制
相关文章

相似问题

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