首页 > 解决方案 > 从分层数据生成分层 URL

问题描述

我正在尝试基于此jQuery 插件实现决策树。它需要这种格式的数据:

$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
                )
            )
        )
    )
);

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

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);

产生:

$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'深度为2,但随后偏离了预期的格式。我不知道如何让'href'正确。

如何使用 jQuery 插件所期望的递归函数生成正确的“href”?

标签: php

解决方案


$path = $current_path . ++$ctr;

您正在计数,但即使在递归时,该值也已针对该特定函数进行了本地化。它永远不会也似乎永远不会超过 1,因为在它迭代一次之后,它会再次调用 buildTree。

q11 是不是没有存入数据库?您需要一种递增/解析/收集的方法(我不确定该值应该是什么)该值在递归时应该是什么。如果这个值只是删除. 1.1开始?

尝试用这个函数代替你的函数,看看它是否改变了什么:

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;
}

推荐阅读