使用PHP

时间:2015-07-09 05:58:17

标签: php json

我有一个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;
    }

function variabl $ tree array value

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

1 个答案:

答案 0 :(得分:-1)

$json_tree = json_encode($tree);

json_encode和json_decode函数将PHP数组转换为JSON对象,然后再返回。