在php中创建Unlimited Unordered列表

时间:2014-12-03 05:34:26

标签: php html arrays multidimensional-array

我试图创建树形mysql生成的数据。

之前已经问过这个问题,但实际上它是关于创建数组的。 因此阵列正在创造良好。我正在使用这种方法的答案。

http://stackoverflow.com/questions/8587341/recursive-function-to-generate-multidimensional-array-from-database-result

这是我得到的数组。

Array
(
    [0] => Array
        (
            [employee_id] => 1
            [organization_id] => 1
            [parent_organization_id] => 0
            [full_name] => Mohammad Salim Khan
            [employee_order] => 1
            [children] => Array
                (
                    [0] => Array
                        (
                            [employee_id] => 2
                            [organization_id] => 2
                            [parent_organization_id] => 1
                            [full_name] => Ali Khan
                            [employee_order] => 2
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [employee_id] => 5
                                            [organization_id] => 4
                                            [parent_organization_id] => 2
                                            [full_name] => Nadeem Khan
                                            [employee_order] => 3
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [employee_id] => 7
                                                            [organization_id] => 5
                                                            [parent_organization_id] => 4
                                                            [full_name] => Sajjad Ali
                                                            [employee_order] => 4
                                                        )

                                                )

                                        )

                                )

                        )

                    [1] => Array
                        (
                            [employee_id] => 3
                            [organization_id] => 3
                            [parent_organization_id] => 1
                            [full_name] => Kamran Durrani
                            [employee_order] => 2
                        )

                )

        )

)

但我现在仍然把这些数据放在<ul>li列表中。

请知道如何把它放在无序列表中,以便我得到这样的列表。

<ul>
<li>Mohammad Salim Khan</li>
<ul>
//1
<li>Ali Khan</li>
<ul>
<li>Nadeem Khan</li>
<ul><li>Sajjad Ali</li></ul>
//2
<li>Kamran Durrani</li>
</ul>
</ul>

更新

  • Muhammad Salim Khan
    • Ali Khan
      • Nadeem Khan
        • Sajjad Ali
    • Kamran Durrani

由于李的无限可能性,它变得非常混乱。

1 个答案:

答案 0 :(得分:1)

这是递归(一个函数调用自身)的情况。从$ myLIst中的列表开始:

function showList ($myList)
{
    echo "<ul>";
    foreach ($myList as $row)
    {
        echo ("<li>{$row ['full_name']}</li>\n");
        if  (isset ($row ['children']))
            showList ($row ['children']);
    }
    echo "</ul>\n";
}