0

我有一个显示组织树的页面,但是在第 2 级有重复的项目。我想要的是避免重复...

这是示例显示:

<ul id="org" style="display: none;">
<li>Brit School                        
    <ul>
        <li>Amy Winehouse
            <ul>
                <li>Carina Round</li>
            </ul>
        </li>
        <li>Adele Adkins
            <ul>
                <li>Kreayshawn K</li>
                <li>Leona Lewis</li>
            </ul>
        </li>
        <li>Adele Adkins
            <ul>
                <li>Kreayshawn K</li>
                <li>Leona Lewis</li>
            </ul>
        <li>Arctic Monkey
            <ul>
                <li>PJ Harvey</li>
            </ul>
        </li>
    </ul>

</li>

我想省略第二条记录 Adele 这是我在 PHP 中的示例代码

<ul id="org" style="display: none;">
<?php foreach ($lvl1 as $genesA) {?>
    <li>
        <?php echo $genesA->LevelFullName1?>
        <?php if($genesA->lvlMemF) {?>
        <ul>
        <?php if($genesA->lvlMemF) {?>
            <?php foreach ($lvl2 as $genesB) {?>
                <?php if($genesB->lvlMemS) {?>
                <li><?php echo $genesB->LevelFullName2 ?>
                <?php if($genesB->lvlMemS) {?>
                  <ul>
                    <?php if($genesB->lvlMemS) {?>
                        <?php foreach ($lvl3 as $genesC) {?>
                            <?php if($genesB->lvlMemS == $genesC->referrerLvl3) {?>
                                <li>
                                    <?php echo $genesC->LevelFullName3?>
                                </li>
                            <?php } ?>
                        <?php } ?>
                    <?php } ?>
                  </ul>
                  <?php } ?>
                </li>
                <?php } else { ?>

                <?php } ?>
            <?php } ?>
        <?php } ?>
        </ul>

        <?php }?>
    </li>
<?php } ?>

我不太确定我是否走在正确的轨道上我只想要一个肮脏的修复就是这样:)

4

1 回答 1

1

应该这样做:

<?php
$seen = array();
?>
<ul id="org" style="display: none;">
<?php foreach ($lvl1 as $genesA) {?>
    <li>
        <?php echo $genesA->LevelFullName1?>
        <?php if($genesA->lvlMemF) {?>
        <ul>
        <?php if($genesA->lvlMemF) {?>
            <?php foreach ($lvl2 as $genesB) {?>
                <?php if($genesB->lvlMemS) {?>
                <li><?php echo $genesB->LevelFullName2 ?>
                <?php if($genesB->lvlMemS) {?>
                  <ul>
                    <?php if($genesB->lvlMemS) {?>
                        <?php foreach ($lvl3 as $genesC) {?>
                            <?php if($genesB->lvlMemS == $genesC->referrerLvl3) {?>
                                <?php if(!in_array($genesC->LevelFullName3, $seen)?>
                                <li>
                                    <?php echo $genesC->LevelFullName3?>
                                    <?php $seen[] =$genesC->LevelFullName3;?>
                                </li>
                                <?php }?>
                            <?php } ?>
                        <?php } ?>
                    <?php } ?>
                  </ul>
                  <?php } ?>
                </li>
                <?php } else { ?>

                <?php } ?>
            <?php } ?>
        <?php } ?>
        </ul>

        <?php }?>
    </li>
</ul>

您可能想检查我的 if 是否在正确的位置,因为ul如果已经看到所有名称,您可能会得到空块。

于 2013-03-18T17:26:53.540 回答