duanjiaolao1187
2015-03-25 21:16
浏览 44
已采纳

如何在smarty中循环访问对象

I have a script which is passing an object array to smarty like so:

$smarty->assign('results', $user->results());

The array looks like this:

Array
(
[0] => stdClass Object
    (
        [id] => 1
        [agency_name] => Agency #1
        [created_date] => 2015-03-25 20:23:44
    )

[1] => stdClass Object
    (
        [id] => 2
        [agency_name] => gggg
        [created_date] => 2015-03-25 21:26:06
    )

)

How can I loop through this object array using smarty? I have tried something like this, with no luck..

{section name="i" loop=$result}
{$result[i]->id}
{/section}

Any ideas?

Thanks

图片转代码服务由CSDN问答提供 功能建议

我有一个脚本将对象数组传递给smarty,如下所示: < pre> $ smarty-&gt; assign('results',$ user-&gt; results());

数组如下所示:< / p>

  Array 
(
 [0] =&gt; stdClass Object 
(
 [id] =&gt; 1 
 [agency_name] =&gt;代理商#1  
 [created_date] =&gt; 2015-03-25 20:23:44 
)
 
 [1] =&gt; stdClass对象
(
 [id] =&gt; 2 
 [agency_name]  =&gt; gggg 
 [created_date] =&gt; 2015-03-25 21:26:06 
)
 
)
   
 
 

我该怎么办? 使用smarty循环遍历此对象数组? 我尝试过这样的事情,没有运气..

  {section name =“i”loop = $ result} 
 {$ result [i]  - &gt; id  } 
 {/ section} 
   
 
 

任何想法?

谢谢

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanhuiuw12555 2015-03-26 11:37
    已采纳

    I managed to get it working using smarty assign_by_ref

    $smarty->assign_by_ref('object', $object)
    
    已采纳该答案
    打赏 评论
  • douaipi3965 2015-03-25 21:39

    // In smarty you can loop through your array like this:

    {foreach from=$results item=result}
    {$result->id}
    {/foreach}
    

    // You can also use "key=xx" and a "{foreachelse}" before the end of the loop in case there's nothing inside the array.

    打赏 评论

相关推荐 更多相似问题