简体   繁体   English

将父母参数添加到递归元素

[英]adding a parents parameter to a recursive element

I'm building a menu and i need to have the parents keys on my generated sub items 我正在建立一个菜单,我需要在生成的子项上有父母键

The function looks like this: 该函数如下所示:

function get_menu($tagmenu){
$menu="";
$count=0;
foreach ($tagmenu as $key => $value) {
    $is_active=false;
    $class="";
    if(isset($_GET["tagsearch"])){
        if($key == $_GET["tagsearch"]){
            $is_aktive=true;
        };
    };
    $menu.= "<ul>";
    $sub="";
    if(is_array($value)){
        if (count($value)>0) {
            $sub.= "<div class='submenu'>";
            $sub.=get_menu($value);
            $sub.= "</div>";
        }
    }

    $li= "<li class='menuitem'><a href='?tagsearch=".$key."'>".$key."</a>";

    if (strpos($sub,"'menuitem active'")!==false || $is_active ) {
        $li=str_replace("'menuitem'", "'menuitem active'", $li);
    }
    $menu.=$li.$sub; 
    $menu.= "</li>";
    $menu.= "</ul>";
}
return $menu;
}

And this is the array; 这是数组;

Array(
    [fotografie] => Array(
            [schwarzweiss] => Array(
                    [street] => Array()

            )

     )

)

Is it possible with this stucture to add all keys of the parent array to the link? 这种结构是否可以将父数组的所有键添加到链接中?

At the end it should look like 最后应该看起来像

<a href="fotografie-schwarzweiss-street"></a>

Yes, it is. 是的。 You need to add second parameter to recursive function, in which you pass current string. 您需要在递归函数中添加第二个参数,在其中传递当前字符串。

function get_menu($tagmenu, $shortcut = array())
{
    $menu="";
    $count=0;
    foreach ($tagmenu as $key => $value) 
    {
        $shortcut[] = $key;
        $is_active=false;
        $class="";
        if(isset($_GET["tagsearch"]))
        {
            if($key == $_GET["tagsearch"])
            {
                $is_aktive=true;
            };
        };
        $menu.= "<ul>";
        $sub="";
        if(is_array($value))
        {
            if (count($value)>0) 
            {
                $sub.= "<div class='submenu'>";
                $sub.=get_menu($value, $shortcut);
                $sub.= "</div>";
            }
        }

        $li= "<li class='menuitem'><a href='?tagsearch=".$key."' class=".implode('-', $shortcut).">".$key."</a>";

        if (strpos($sub,"'menuitem active'")!==false || $is_active ) 
        {
            $li=str_replace("'menuitem'", "'menuitem active'", $li);
        }
        $menu.=$li.$sub; 
        $menu.= "</li>";
        $menu.= "</ul>";
    }

    return $menu;
}

echo get_menu(
    array(
        'fotografie' => array(
            'schwarzweiss' => array(
              'street' => array()
            )
        )
    )
);

You have had already href attribute on the link, so I've used class attr to show you the example. 您已经在链接上具有href属性,因此我使用了class attr来向您展示示例。

Demo: http://sandbox.onlinephpfunctions.com/code/184eaa428de75511eda1ffee8f8ad08b82a03919 演示: http : //sandbox.onlinephpfunctions.com/code/184eaa428de75511eda1ffee8f8ad08b82a03919

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM