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] => Array
(
[new_window] => 0
[item] => Butterfly Sofa Bean Bags
[href] => categories.view?category_id=114
[level] => 2
)
[123] => Array
(
[new_window] => 0
[item] => Avatar Lounger Bean Bags
[href] => categories.view?category_id=123
[level] => 2
)
[115] => Array
(
[new_window] => 0
[item] => Twin Couch Bean Bags
[href] => categories.view?category_id=115
[level] => 2
)
[116] => Array
(
[new_window] => 0
[item] => Versa Table Bean Bags
[href] => categories.view?category_id=116
[level] => 2
)
[113] => Array
(
[new_window] => 0
[item] => Acoustic Sofa Bean Bags
[href] => categories.view?category_id=113
[level] => 2
)
[164] => Array
(
[new_window] => 0
[item] => Twin Ottoman
[href] => categories.view?category_id=164
[level] => 2
)
[136] => Array
(
[new_window] => 0
[item] => Wing Ottoman Bean Bags
[href] => categories.view?category_id=136
[level] => 2
)
[112] => Array
(
[new_window] => 0
[item] => Fiorenze Bean Bags
[href] => categories.view?category_id=112
[level] => 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}
我对 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] => Array
(
[new_window] => 0
[item] => Butterfly Sofa Bean Bags
[href] => categories.view?category_id=114
[level] => 2
)
[123] => Array
(
[new_window] => 0
[item] => Avatar Lounger Bean Bags
[href] => categories.view?category_id=123
[level] => 2
)
[115] => Array
(
[new_window] => 0
[item] => Twin Couch Bean Bags
[href] => categories.view?category_id=115
[level] => 2
)
[116] => Array
(
[new_window] => 0
[item] => Versa Table Bean Bags
[href] => categories.view?category_id=116
[level] => 2
)
[113] => Array
(
[new_window] => 0
[item] => Acoustic Sofa Bean Bags
[href] => categories.view?category_id=113
[level] => 2
)
[164] => Array
(
[new_window] => 0
[item] => Twin Ottoman
[href] => categories.view?category_id=164
[level] => 2
)
[136] => Array
(
[new_window] => 0
[item] => Wing Ottoman Bean Bags
[href] => categories.view?category_id=136
[level] => 2
)
[112] => Array
(
[new_window] => 0
[item] => Fiorenze Bean Bags
[href] => categories.view?category_id=112
[level] => 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}