从多维PHP数组构建树

时间:2013-10-02 19:23:46

标签: php arrays

我正在尝试使用下面这种格式构建一个HTML树,它可以根据需要深入多个级别,但它应该遵循这种格式。

我作为数据源所拥有的是一个PHP数组,它是通过Magento中的API调用构建的。

我的PHP数组看起来像这样......

Array
(
    [category_id] => 1
    [parent_id] => 0
    [name] => Root Catalog
    [is_active] => 
    [position] => 0
    [level] => 0
    [children] => Array
        (
            [0] => Array
                (
                    [category_id] => 2
                    [parent_id] => 1
                    [name] => Default Category
                    [is_active] => 1
                    [position] => 1
                    [level] => 1
                    [children] => Array
                        (
                            [0] => Array
                                (
                                    [category_id] => 263
                                    [parent_id] => 2
                                    [name] => Texas Roadhouse
                                    [is_active] => 1
                                    [position] => 1
                                    [level] => 2
                                    [children] => Array
                                        (
                                        )

                                )

                            [1] => Array
                                (
                                    [category_id] => 265
                                    [parent_id] => 2
                                    [name] => Bubba's
                                    [is_active] => 1
                                    [position] => 2
                                    [level] => 2
                                    [children] => Array
                                        (
                                        )

                                )

                            [2] => Array
                                (
                                    [category_id] => 3
                                    [parent_id] => 2
                                    [name] => Neon Signs
                                    [is_active] => 1
                                    [position] => 3
                                    [level] => 2
                                    [children] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [category_id] => 12
                                                    [parent_id] => 3
                                                    [name] => Custom Neon Signs
                                                    [is_active] => 1
                                                    [position] => 1
                                                    [level] => 3
                                                    [children] => Array
                                                        (
                                                            [0] => Array
                                                                (
                                                                    [category_id] => 257
                                                                    [parent_id] => 12
                                                                    [name] => Animals Custom Neon Signs
                                                                    [is_active] => 0
                                                                    [position] => 1
                                                                    [level] => 4
                                                                    [children] => Array
                                                                        (
                                                                        )

                                                                )

                                                            [1] => Array
                                                                (
                                                                    [category_id] => 258
                                                                    [parent_id] => 12
                                                                    [name] => Bar Beer Cocktails Custom Neon Signs
                                                                    [is_active] => 0
                                                                    [position] => 2
                                                                    [level] => 4
                                                                    [children] => Array
                                                                        (
                                                                        )

                                                                )

我需要将我的PHP数组转换成的HTML输出应采用这种格式...

<div id="tree">
    <ul>
        <li><input type="checkbox"><span>Node 1</span>
            <ul>
                <li><input type="checkbox"><span>Node 1.1</span>
                    <ul>
                        <li><input type="checkbox"><span>Node 1.1.1</span>
                    </ul>
            </ul>
            <ul>
                <li><input type="checkbox"><span>Node 1.2</span>
                    <ul>
                        <li><input type="checkbox"><span>Node 1.2.1</span>
                        <li><input type="checkbox"><span>Node 1.2.2</span>
                        <li><input type="checkbox"><span>Node 1.2.3</span>
                            <ul>
                                <li><input type="checkbox"><span>Node 1.2.3.1</span>
                                <li><input type="checkbox"><span>Node 1.2.3.2</span>
                            </ul>
                        <li><input type="checkbox"><span>Node 1.2.4</span>
                        <li><input type="checkbox"><span>Node 1.2.5</span>
                        <li><input type="checkbox"><span>Node 1.2.6</span>
                    </ul>
            </ul>
        <li><input type="checkbox"><span>Node 2</span>
            <ul>
                <li><input type="checkbox"><span>Node 2.1</span>
                    <ul>
                        <li><input type="checkbox"><span>Node 2.1.1</span>
                    </ul>
                <li><input type="checkbox"><span>Node 2.2</span>
                    <ul>
                        <li><input type="checkbox"><span>Node 2.2.1</span>
                        <li><input type="checkbox"><span>Node 2.2.2</span>
                        <li><input type="checkbox"><span>Node 2.2.3</span>
                            <ul>
                                <li><input type="checkbox"><span>Node 2.2.3.1</span>
                                <li><input type="checkbox"><span>Node 2.2.3.2</span>
                            </ul>
                        <li><input type="checkbox"><span>Node 2.2.4</span>
                        <li><input type="checkbox"><span>Node 2.2.5</span>
                        <li><input type="checkbox"><span>Node 2.2.6</span>
                    </ul>
            </ul>
    </ul>
</div>

我真的不知道如何实现这一目标,我知道我需要一遍又一遍地重复数组,但我不知道如何做到这一点,如果有人可以提供帮助,我将非常感谢您的时间和知识< / p>

1 个答案:

答案 0 :(得分:3)

您可以使用递归函数:

function buildTree($data, $parentLevel = "") {
    echo "<ul>";

    $nodeCount = 1;
    foreach ($data as $value) {
        if ($parentLevel != "") {
            $currentLevel = $parentLevel . "." . $nodeCount;
        } else {
            $currentLevel = $nodeCount;
        }

        // Output the list element
        echo "<li><input type=\"checkbox\"><span>Node " . $parentLevel . "</span></li>";

        if (isset($value["children"]) && count($value["children"]) > 0) {
            buildTree($value["children"], $parentLevel . "." . $nodeCount);
        }

        $nodeCount++;
    }
    echo "</ul>";
}