duanguai2781 2015-08-09 05:48
浏览 72
已采纳

smarty v3在foreach中显示嵌套数组元素

I have searched but can't seem to find a working solution to my problem.

I am trying to pull only 2 elements from an array: $menu.contents.text and $menu.contents.url

The arrays var is Menu if you haven't yet guessed. This is what I get when I run my code.

This is the code I use at the moment:

<form name='jump'>
<select name='menu' onChange='location=document.jump.menu.options[document.jump.menu.selectedIndex].value;' size='1' value='GO'>
{foreach from=$menu item=result}
    {if $result.contents}
        {foreach from=$result.contents item=mc}
            <option value="{$mc.url}">{$mc.text}</option>
        {/foreach}
    {/if}
{/foreach}
</select>
</form>

This is the array I am trying to grab it from:

array(24)
{ 
["id"]=> string(2) "91" 
["title"]=> string(11) "Quick Links" 
["name"]=> string(11) "yellowpages" 
["contents"]=> array(1)
{ 
    [0]=> array(5) 
    { 
        [97]=> array(13) 
        { 
            ["id"]=> string(2) "97" 
            ["menu_id"]=> string(2) "91" 
            ["parent_id"]=> string(1) "0" 
            ["el_id"]=> string(7) "81_6b3a" 
            ["page_name"]=> string(10) "new_ypages" 
            ["level"]=> string(1) "0" 
            ["nofollow"]=> string(1) "0" 
            ["new_window"]=> string(1) "0" 
            ["action"]=> string(4) "read" 
            ["custom_url"]=> string(0) "" 
            ["active"]=> bool(false) 
            ["text"]=> string(13) "New Companies" 
            ["url"]=> string(51) "dealers/new_companies/" 
        }

        [98]=> array(13) 
        { 
            ["id"]=> string(2) "98" 
            ["menu_id"]=> string(2) "91" 
            ["parent_id"]=> string(1) "0" 
            ["el_id"]=> string(7) "82_cae6" 
            ["page_name"]=> string(10) "top_ypages" 
            ["level"]=> string(1) "0" 
            ["nofollow"]=> string(1) "0" 
            ["new_window"]=> string(1) "0" 
            ["action"]=> string(4) "read" 
            ["custom_url"]=> string(0) "" 
            ["active"]=> bool(false) 
            ["text"]=> string(13) "Top Companies" 
            ["url"]=> string(51) "dealers/top_companies/" 
        }
    }
}
["order"]=> string(1) "1"
["position"]=> string(4) "left"
["type"]=> string(4) "menu"
["extras"]=> string(11) "yellowpages"
["status"]=> string(6) "active"
["header"]=> string(1) "0"
["collapsible"]=> string(1) "1"
["collapsed"]=> string(1) "0"
["sticky"]=> string(1) "0"
["rss"]=> string(0) ""
["multilingual"]=> string(1) "0"
["lang"]=> string(2) "en"
["tpl"]=> string(15) "render-menu.tpl"
["external"]=> string(1) "0"
["filename"]=> NULL
["removable"]=> string(1) "0"
["subpages"]=> string(0) ""
["classname"]=> string(0) ""
["display"]=> bool(true)
["hidden"]=> int(0) 0
}

Does anyone see what I am doing wrong?

  • 写回答

1条回答 默认 最新

  • dongle0396 2015-08-09 06:07
    关注

    I think the first foreach is not in the right place. Try this:

    {if !empty($menu.contents)}
      {foreach from=$menu.contents item=contentsItem} {* Loop through the contents array *}
        {foreach from=$contentsItem item=mc}
          <option value="{$mc.url}">{$mc.text}</option>
        {/foreach}
      {/foreach}
    {/if}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?