duan117890 2014-12-15 10:35
浏览 90
已采纳

在smarty中访问嵌套数组

I am trying to access a nested array inside html code using smarty. For example i have an array that looks like this: Array(customer)[name, age, id[firstname, lastname, birthdate]]. I have been trying something like this:

{foreach from=$customer item=foo} 
    Customer id: {$foo.id} 
{/foreach}

But this doesent print any values to the page. However with this syntax

{foreach from $customer item=foo}
    {$foo.id} // or {$foo.id.0}
{/foreach}

It prints out: Array

How do i access the values of nested array using smarty?

  • 写回答

2条回答 默认 最新

  • dounao1856 2014-12-15 10:39
    关注

    Try this:

    {foreach from=$customer item=foo} 
        Customer name: {$foo.id.firstname} {$foo.id.lastname}
    {/foreach}
    

    Smarty will compile it to something equivalent with:

    foreach ($customer as $foo) {
       echo('Customer name: '.$foo['id']['firstname'].' '.$foo['id']['laststname']);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?