PHP树更改父结构

时间:2019-02-28 19:35:30

标签: php html arrays tree treeview

我想以其他方式(如Wordpress)更改父级的结构,并在父级添加类。

当前结构:

<ul>
    <li>My List
        <ul>
            <li>Menu 1</li>
            <ul>
                <li>Menu 2</li>
                <ul>
                    <li>Menu 3</li>
                </ul>
                <li>Menu 4</li>
            </ul>
            <li>Menu 5</li>
        </ul>
    </li>

我想要什么:

<ul>
    <li>My List
        <li>Menu 1
            <ul class="sub-menu">
                <li>Menu 2
                    <ul class="sub-menu">
                        <li>Menu 3</li>
                    </ul>
                </li>
                <li>Menu 4</li>
            </ul>
        </li>
        <li>Menu 5</li>
    </li>
</ul>

代码:

$lists = array(
    array(
        'id' => 1,
        'parent' => 0,
        'name' => 'Menu 1',
    ),
    array(
        'id' => 2,
        'parent' => 1,
        'name' => 'Menu 2',
    ),
    array(
        'id' => 3,
        'parent' => 2,
        'name' => 'Menu 3',
    ),
    array(
        'id' => 4,
        'parent' => 1,
        'name' => 'Menu 4',
    ),
    array(
        'id' => 5,
        'parent' => 0,
        'name' => 'Menu 5',
    )
);
?>
<ul>
    <li>My List
    <?php
    function make_list($lists, $parent = 0) {
        $children = array_filter($lists, function ($v) use($parent) { return $v['parent'] == $parent; });
        if(!count($children)) return;
        echo "<ul>";
        foreach ($children as $child) {
            echo "<li>{$child['name']}</li>";
            make_list($lists, $child['id']);
        }
        echo "</ul>";
    }
    make_list($lists);
    ?>
    </li>
</ul>

2 个答案:

答案 0 :(得分:1)

您可以对跟踪菜单级别的功能进行较小的更改来做到这一点:

function make_list($lists, $parent = 0, $level = 0) {
    $children = array_filter($lists[0], function ($v) use($parent) { return $v['parent'] == $parent; });
    if (!count($children)) return;
    echo "<ul" . ($level > 0 ? ' class="sub-menu"' : '') . ">\n";
    foreach ($children as $child) {
        echo "<li>{$child['name']}</li>\n";
        make_list($lists, $child['id'], ++$level);
    }
    echo "</ul>\n";
}
make_list($lists);

答案 1 :(得分:0)

尝试此代码,虽然不理想,但可以使用

$items = [
    1 => [
        'name' => 'Menu 1',
        'children' => [2,4],
        'isChild' => false
    ],
    2 => [
        'name' => 'Menu 2',
        'children' => [3],
        'isChild' => true
    ],
    3 => [
        'name' => 'Menu 3',
        'children' => [],
        'isChild' => true
    ],
    4 => [
        'name' => 'Menu 4',
        'children' => [],
        'isChild' => true
    ],
    5 => [
        'name' => 'Menu 5',
        'children' => [],
        'isChild' => false
    ]
];



$html = '<ul>Menu list';
foreach($items as $key => $item) {
    $html .= buildMenu($item, $items);
}
$html .= '</ul>';

echo $html;

function buildMenu($item, $items, $listAsParent = true) {
        if($listAsParent && $item['isChild']) {
            return;
        }
        $html = '<li>'.$item['name'];
        if(count($item['children']) != 0) {
            $html .= '<ul class="sub-menu">';
            foreach($item['children'] as $child) {
                $html .= buildMenu($items[$child], $items, false);
            }
            $html .= '</ul>';
        }
        $html .= '</li>';
    return $html;
}