具有许多子的多维数组

时间:2014-06-16 12:24:10

标签: php multidimensional-array

大家好!

任何人都可以帮我创建这个多维数组吗?

$page_nav = array(
"dashboard" => array(
    "title" => "Dashboard",
    "url" => "ajax/dashboard.php",
    "icon" => "fa-home"
),
"inbox" => array(
    "title" => "Inbox",
    "url" => "ajax/inbox.php",
    "icon" => "fa-inbox",
    "label_htm" => '<span class="badge pull-right inbox-badge">14</span>'
),
"tables" => array(
    "title" => "Tables",
    "icon" => "fa-table",
    "sub" => array(
        "normal" => array(
            "title" => "Normal Tables",
            "url" => "ajax/table.php"
        ),
        "data" => array(
            "title" => "Data Tables",
            "url" => "ajax/datatables.php"
        )
    )
),
"ui_elements" => array(
    "title" => "UI Elements",
    "icon" => "fa-desktop",
    "sub" => array(
        "general" => array(
            "title" => "General Elements",
            "url" => "ajax/general-elements.php"
        ),
        "buttons" => array(
            "title" => "Buttons",
            "url" => "ajax/buttons.php"
        ),
        "icons" => array(
            "title" => "Icons",
            "sub" => array(
                "fa" => array(
                    "title" => "Font Awesome",
                    "icon" => "fa-plane",
                    "url" => "fa.php"
                ),
                "glyph" => array(
                    "title" => "Glyph Icons",
                    "icon" => "fa-plane",
                    "url" => "glyph.php"
                )
            ) 
        ),
        "grid" => array(
            "title" => "Grid",
            "url" => "ajax/grid.php"
        ),
        "tree_view" => array(
            "title" => "Tree View",
            "url" => "ajax/treeview.php"
        ),
        "nestable_lists" => array(
            "title" => "Nestable Lists",
            "url" => "ajax/nestable-list.php"
        ),
        "jquery_ui" => array(
            "title" => "jQuery UI",
            "url" => "ajax/jqui.php"
        )
    )
)

);

我正在执行以下操作,但仅生成2d。

    $sQuery = "SELECT * FROM tb_menu";
$rResult = $db->query( $sQuery );

$tree = array();
while($row = $rResult->fetch_assoc()) {
    if($row['parent'] == 0)
    {
        $row['sub'] = array();
        $tree[$row['id']] = $row;
    }
    else {
        $tree[$row['parent']]['sub'][] = $row;
    }
}

$page_nav = $tree;

表格结构

|id|parent|name |title |url  |icon|label_html|
|1 |0     |item1|Item 1|1.php|fa-1|null      |
|2 |0     |item2|Item 2|2.php|fa-2|null      |
|3 |0     |item3|Item 3|3.php|fa-3|null      |
|4 |3     |item4|Item 4|4.php|fa-4|null      |
|5 |3     |item5|Item 5|5.php|fa-5|null      |
|6 |0     |item6|Item 6|6.php|fa-6|null      |
|7 |6     |item7|Item 7|7.php|fa-7|null      |
|8 |6     |item8|Item 8|8.php|fa-8|null      |
|9 |8     |item9|Item 9|9.php|fa-9|null      |

0 个答案:

没有答案