Smarty 循环获取数组 ID/Key

Smarty loop getting the array ID/Key

我对 Smarty 和循环等有点陌生。我有这些数据,但需要在子数组之前获取初始 ID,例如 [114]、[115],但不确定如何获取。有什么想法吗?

我是这样输出的:

<pre>{$item2.$childs|print_r}</pre>

我需要从我使用的打印中获取数组键,因为它是我需要获取的 ID:

<ul class="ty-menu__submenu-list cm-responsive-menu-submenu">
    {if $item2.$childs}
        {hook name="blocks:topmenu_dropdown_3levels_col_elements"}
        {foreach from=$item2.$childs key="i" item="item3" name="item3"}
            {assign var="item3_url" value=$item3|fn_form_dropdown_object_link:$block.type}
            {assign var="item3_id" value=$item3[id]|fn_check_is_active_menu_item:$block.type}
            <pre>{$item2.$childs|print_r}</pre>
            <li class="ty-menu__submenu-item{if $item3.active || $item3|fn_check_is_active_menu_item:$block.type} ty-menu__submenu-item-active{/if}{if $item3.class} {$item3.class}{/if}" data-mm-id="{$item3_id}">
                  <a{if $item3_url} href="{$item3_url}"{/if}
                        class="ty-menu__submenu-link">{$item3.$name}</a>
            </li>
        {/foreach}
        {if $item2.show_more && $item2_url}
            <li class="ty-menu__submenu-item ty-menu__submenu-alt-link">
                <a href="{$item2_url}"
                   class="ty-menu__submenu-link">{__("text_topmenu_view_more")}</a>
            </li>
        {/if}
        {/hook}
    {/if}
</ul>

数组打印

Array
(
    [114] =&gt; Array
        (
            [new_window] =&gt; 0
            [item] =&gt; Butterfly Sofa Bean Bags
            [href] =&gt; categories.view?category_id=114
            [level] =&gt; 2
        )

    [123] =&gt; Array
        (
            [new_window] =&gt; 0
            [item] =&gt; Avatar Lounger Bean Bags
            [href] =&gt; categories.view?category_id=123
            [level] =&gt; 2
        )

    [115] =&gt; Array
        (
            [new_window] =&gt; 0
            [item] =&gt; Twin Couch Bean Bags
            [href] =&gt; categories.view?category_id=115
            [level] =&gt; 2
        )

    [116] =&gt; Array
        (
            [new_window] =&gt; 0
            [item] =&gt; Versa Table Bean Bags
            [href] =&gt; categories.view?category_id=116
            [level] =&gt; 2
        )

    [113] =&gt; Array
        (
            [new_window] =&gt; 0
            [item] =&gt; Acoustic Sofa Bean Bags
            [href] =&gt; categories.view?category_id=113
            [level] =&gt; 2
        )

    [164] =&gt; Array
        (
            [new_window] =&gt; 0
            [item] =&gt; Twin Ottoman
            [href] =&gt; categories.view?category_id=164
            [level] =&gt; 2
        )

    [136] =&gt; Array
        (
            [new_window] =&gt; 0
            [item] =&gt; Wing Ottoman Bean Bags
            [href] =&gt; categories.view?category_id=136
            [level] =&gt; 2
        )

    [112] =&gt; Array
        (
            [new_window] =&gt; 0
            [item] =&gt; Fiorenze Bean Bags
            [href] =&gt; categories.view?category_id=112
            [level] =&gt; 2
        )

)

你可以这样获取数组

$array = array(
'9' => array('4','6','8'),
'1' => '2',
'2' => '0',
'3' => '3',
'4' => '1');

foreach($array as $key => $value)
{
  echo $mykey = $key;
}

解决方案是设置一个密钥并像这样输出

{foreach from=$item2.$childs key=item3_id item="item3" name="item3"}
    {assign var="item3_url" value=$item3|fn_form_dropdown_object_link:$block.type}
    <li class="ty-menu__submenu-item{if $item3.active || $item3|fn_check_is_active_menu_item:$block.type} ty-menu__submenu-item-active{/if}{if $item3.class} {$item3.class}{/if}" data-mm-id="{$item3_id}">
          <a{if $item3_url} href="{$item3_url}"{/if}class="ty-menu__submenu-link">{$item3.$name}</a>
    </li>
{/foreach}

输出

{$item3_id}