将分层数据(右-左)转换为json";父母子女";使用PHP


convert Hierarchical Data (right-left) to json "parent children" using PHP

我有一个php系统来生成菜单,问题是它用html生成菜单,我需要json"父子"中的数据

我需要这个无限级别的

[{"name":"Cat 1","value":"value1","children":[{"name":"sub 2", "value":"value2","children":[{"name":"sub 3", "value":"value 3"}]}]}, {"name":"Cat 2","value":"value 2","children":[{"name":"sub 1", "value":"value1"}]},{"name":"item","value":"value1"}]

生成html代码的函数

    public function render_menu($safe_name, $ol_id, $orientation = "horizontal", $color = "") {
        $menuDetails = $this->get_menuDetails($safe_name);
        $tree = $this->get_menuItems($menuDetails["menu_id"]);
        if ($tree == false ) {
            $result = "Menu has no items available";
        } else {
            // bootstrap loop
            $result         = '<ol id="'. $ol_id.'" class="simple_menu simple_menu_css '.$orientation.' '.$color.'">';
            $currDepth      = 0;                // Start from what depth (0 to get the outer <ul> - NOT RECOMENDED)
            $total          = count($tree) - 1; // Total of menu items (according to previous first foreach loop above) minus one because we have a 0 key in array eith no menu items
            $x              = 0;                // Reset X for counting next while loop
            $Dep1Count      = 0;                // Counter for depth 1 items
            $depth1         = $this->get_countDepth($menuDetails["menu_id"], 1);
            while (!empty($tree)) {
                $x++;
                $currNode = array_shift($tree);
                if ($currNode['depth'] == 1) { $Dep1Count++; };
                // Level down?
                if ($currNode['depth'] > $currDepth) {
                    // Yes, open <ul>
                    $result .= '<ol>';
                } else if ($x > 1) {
                    $result .= '</li>';
                }
                // Level up?
                if ($currNode['depth'] < $currDepth) {
                    // Yes, close n open <ul>
                    $result .= str_repeat('</ol></li>', $currDepth - $currNode['depth']);
                }

                // Checks if it is last item OR last depth one item
                if (($x == $total) or ($Dep1Count == $depth1)) { 
                    $lastClass = "last"; 
                } else {
                    $lastClass = ""; 
                }

                // What to do with each type of link ?
                if ($currNode['type'] == "url") {
                    $url = $this->get_menuItem_URL($currNode['id']);
                } else if ($currNode['type'] == "component") {
                    $url = $this->get_menuItem_COMP($currNode['id']);
                } else if ($currNode['type'] == "content") {
                    $url = $this->get_menuItem_CONT($currNode['id']);
                } else {
                    $url = "#";
                }

                // Always add node. Checks if it is last item OR last depth one item
                if ($x == $total) {
                    $result     .= '<li class="'.$lastClass.'"><a href="'.$url.'">'.$currNode['title'].'</a></li>';
                } else {
                    $result     .= '<li class="'.$lastClass.'"><a href="'.$url.'">'.$currNode['title'].'</a>';
                }
                // Adjust current depth
                $currDepth  = $currNode['depth'];
                // Are we finished?
                if (empty($tree)) {
                    // Yes, close n open <ul>'s and correspondig <li>'s
                    $result .= str_repeat('</ol></li>', $currDepth);
                }
            }
            $result .= "</ol>";

            $result .= "<br clear='"all'" />
                        <script>
                            $('ol#".$ol_id."').simpleMenu();
                        </script>";
        }
        $output = $result;
        return $output;
    }

函数变量$tree数组值

array(6) {
  [0]=>
  array(8) {
    ["title"]=>
    string(5) "cat 1"
    ["type"]=>
    string(3) "url"
    ["class_name"]=>
    string(0) ""
    ["content"]=>
    string(6) "value1"
    ["id"]=>
    string(3) "122"
    ["lft"]=>
    string(1) "1"
    ["rgt"]=>
    string(1) "6"
    ["depth"]=>
    string(1) "0"
  }
  [1]=>
  array(8) {
    ["title"]=>
    string(5) "sub 2"
    ["type"]=>
    string(3) "url"
    ["class_name"]=>
    string(0) ""
    ["content"]=>
    string(6) "value2"
    ["id"]=>
    string(3) "123"
    ["lft"]=>
    string(1) "2"
    ["rgt"]=>
    string(1) "5"
    ["depth"]=>
    string(1) "1"
  }
  [2]=>
  array(8) {
    ["title"]=>
    string(5) "sub 3"
    ["type"]=>
    string(3) "url"
    ["class_name"]=>
    string(0) ""
    ["content"]=>
    string(6) "value3"
    ["id"]=>
    string(3) "124"
    ["lft"]=>
    string(1) "3"
    ["rgt"]=>
    string(1) "4"
    ["depth"]=>
    string(1) "2"
  }
  [3]=>
  array(8) {
    ["title"]=>
    string(5) "cat 2"
    ["type"]=>
    string(3) "url"
    ["class_name"]=>
    string(0) ""
    ["content"]=>
    string(6) "value2"
    ["id"]=>
    string(3) "125"
    ["lft"]=>
    string(1) "7"
    ["rgt"]=>
    string(2) "10"
    ["depth"]=>
    string(1) "0"
  }
  [4]=>
  array(8) {
    ["title"]=>
    string(5) "sub 1"
    ["type"]=>
    string(3) "url"
    ["class_name"]=>
    string(0) ""
    ["content"]=>
    string(6) "value1"
    ["id"]=>
    string(3) "126"
    ["lft"]=>
    string(1) "8"
    ["rgt"]=>
    string(1) "9"
    ["depth"]=>
    string(1) "1"
  }
  [5]=>
  array(8) {
    ["title"]=>
    string(4) "item"
    ["type"]=>
    string(3) "url"
    ["class_name"]=>
    string(0) ""
    ["content"]=>
    string(6) "value1"
    ["id"]=>
    string(3) "127"
    ["lft"]=>
    string(2) "11"
    ["rgt"]=>
    string(2) "12"
    ["depth"]=>
    string(1) "0"
  }
}
$json_tree = json_encode($tree);

json_encode和json_edecode函数将PHP数组转换为json对象,然后再转换回来。