James James - 10 months ago 124
Smarty Question

Smarty loop getting the array ID/Key

I am a little new with Smarty and loops, etc. I have this data coming through but need to get the initial ID such as [114], [115] before the child array and not sure how. Any ideas?

I am outputting like so:

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


I need to get the array Key from the print I used, as it is the ID I need to get:

<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>


Print of array

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
)

)

Answer Source

Solution was setting a key and outputting like so

{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}

output with

{$item3_id}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download