来自widget菜单的yii2动态子菜单

时间:2015-01-22 17:32:29

标签: php yii2

我需要理解的是,如果我声明了我想要的值,我总是会生成具有0索引的生成数组 我想要这个输出

    Array
(
    [0] => Array
    (
        [label] => Prima categoria,
        [url] => Array
        (
            [0] => categorie/prima-categoria
        )
    )

[1] => Array
            (
    [label] => Seconda
    [url] => Array
               (
            [0] => categorie/seconda
               )
            [items] => Array
                  (
                    [label] => Seconda
                    [url] => Array
                  (
             [0] => categorie/seconda
            )
        )
    )
)

但现在:

   Array
(
    [0] => Array
        (
            [label] => Prima categoria,
            [url] => Array
                (
                    [0] => categorie/prima-categoria
                )
        )
    [1] => Array
        (
            [label] => Seconda
            [url] => Array
                (
                    [0] => categorie/seconda
                )
            // this is the problem --- why 0??
            [0] => Array
                (
                    [items] => Array
                        (
                            [label] => Seconda
                            [url] => Array
                                (
                                    [0] => categorie/seconda
                                )

                        )
                )
        )
    )

我正在尝试实施的功能

function getItems() {
$items = [];
foreach (getLabelName() as $key) {
    $url = str_replace(' ', '-', $key->name);
    $subMenu = [];
    if (isset($key->id_sub_cat)) {
        $subMenu['items'] = ['label' => ucfirst($key->name), 'url' => ['category/' . $url . '']];
    }

    $items[] = ['label' => ucfirst($key->name), 'url' => ['category/' . $url . ''], $subMenu];
}
return $items;

}

希望你理解我的解释。

2 个答案:

答案 0 :(得分:0)

因此:

$items[] = ['label' => ucfirst($key->name), 'url' => ['category/' . $url . ''], $subMenu];

你应该做的是

function getItems() {
$items = [];
foreach (getLabelName() as $key) {
    $url = str_replace(' ', '-', $key->name);
    $subMenu = [];
    if (isset($key->id_sub_cat)) {
        $subMenu[] = ['label' => ucfirst($key->name), 'url' => ['category/' . $url . '']];
    }

    $items[] = ['label' => ucfirst($key->name), 'url' => ['category/' . $url . ''], 'items' => $subMenu];
}
return $items;
}

答案 1 :(得分:-1)

foreach(Menu::getArrayMenu() as $item){
    $menuItems[] = $item;
}

public static function getArrayMenu(){

    $array[] = ['label' => 'Онас', 'url' => ['/site/about']];
    $array[] = ['label' => 'Контакты', 'url' => ['/site/contacts']];

    return $array;
}