如何在PHP中使用数组从所有顶级列表项中删除嵌套的导航(除了一个)?

k7fdbhmy  于 2023-03-21  发布在  PHP
关注(0)|答案(1)|浏览(141)

我想删除名为$subPages的嵌套列表,使其不再显示在所有名为$pages的顶级列表(服务除外)下。
我的尝试是允许嵌套列表项显示在所有三个顶级项下。

如何从所有顶层列出的项目中删除嵌套导航,只有一个例外,即“Services”仅在PHP中使用数组?

<nav class="navBar wAuto fRight positAb">
            <?php
                $pages = array(
                '/about/' => 'About',
                '/services/' => 'Services',
                '/book-a-service/' => 'Book a service'
                );
    
                $subPages = array(
                '/services/bathroom-installation' => 'Bathroom installation',
                '/services/boiler-repair' => 'Boiler repair',
                '/services/boiler-service' => 'Boiler service',
                '/services/gas-oil' => 'Gas &amp; oil',
                '/services/heat-recovery' => 'Heat recovery',
                '/services/heating-plumbing' => 'Heating and plumbing',
                '/services/rainwater-harvesting' => 'Rainwater harvesting',
                '/services/solar-solutions' => 'Solar solutions',
                '/services/underfloor-heating' => 'Underfloor heating'
                );
                $activePage = $_SERVER['REQUEST_URI'];
                //open nav
    
            foreach( $pages as $url => $anchor){
                $activeClass = ($_SERVER['REQUEST_URI'] == $url) ? " active" : "";
                echo '<li class="fLeft"><a class="font13'.$activeClass.'" href="'.$url.'"';
    
                if($url == '/services/'){
                    echo 'id="parent"';
                };
    
                if($url == '/book-a-service/'){
                    echo 'id="ctaUHP"';
                    echo '>'.$anchor.'</a>';
                    //Open nested nav
                };
    
                if(isset($url) == '/services/'){
                    echo '<ul class="wAuto positAb">';
                    foreach($subPages as $url => $anchor){
                        echo '<li class="fullWidth"><a class="font10" href="'.$url.'"';//open nested nav
                        echo '>'.$anchor.'</a></li>';
                    }
                };
    
                //Close nested nav
                echo '</li></ul>';
                //Close submenu
            }
    
            //Close nav
            echo '</ul>';
         ?>
        </nav>
nx7onnlm

nx7onnlm1#

在打开嵌套导航的代码中,将if(isset($url) == '/services/')更改为if($url == '/services/'){

**说明:**您不需要验证是否设置了$url,您只需要在您的$url == '/services/'时打印嵌套的nav,这就是您需要删除函数isset()的原因:)
编辑

您缺少一些{},我甚至重新排列了您的一些代码,所以现在看起来像这样:)

echo '<ul>'; //open nav

foreach( $pages as $url => $anchor) {
    $activeClass = ($_SERVER['REQUEST_URI'] == $url) ? " active" : "";

    echo '<li class="fLeft"><a class="font13'.$activeClass.'" href="'.$url.'"';

    if($url == '/services/') {
        echo 'id="parent"';
    }

    if($url == '/book-a-service/') {
        echo 'id="ctaUHP"';
    }

    echo '>'.$anchor.'</a>';

    //verify if exists nested nav
    if($url == '/services/') {
        echo '<ul class="wAuto positAb">'; //open nested nav

        foreach ($subPages as $url => $anchor) {
            echo '<li class="fullWidth"><a class="font10" href="'.$url.'">'.$anchor.'</a></li>';
        }

        echo '</ul>'; //close nested nav
     }

     echo '</li>';
}

echo '</ul>'; //close nav

相关问题