在 smarty 中打印数组中的子数组(foreach)

发布于 2024-11-10 12:08:32 字数 1377 浏览 3 评论 0原文

我正在尝试使用 smarty 模板引擎打印数组中的子数组。假设我有一个像这样的数组结构:

Array
    (
[0] => Array
    (
        [directory] => One Day As A Lion - 2011-03-01/
        [source_notes] => iRiver
        [recording_type] => Audience
        [type] => Audio
        [source_type] => MP3
        [fileList] => Array
            (
                [0] => 09 One Day As A Lion.mp3
                [1] => 01 Intro.mp3
                [2] => 05 Ocean View.mp3
                [3] => 04 Swashbuckler.mp3
                [4] => One Day As A Lion - 2011-03-01 - Prince Bandroom, Melbourne, Australia.jpg
                [5] => 10 Peart Plus.mp3
                [6] => 06 Rockers.mp3
                [7] => 03 Last Letter.mp3
                [8] => 07 Swampy.mp3
                [9] => 02 If You Fear Dying.mp3
                [10] => 08 Wild International.mp3
            )

    )

)

我究竟如何获取包含要在 smarty 中打印的文件名的数组?目前我在 smarty 中有一个 foreach 循环,如下所示:

  {foreach $sources as $sourceInfo}
  {strip}       
    Recording Type: {$sourceInfo.type} : {$sourceInfo.recording_type}<br>
    Source : {$sourceInfo.source_notes}<br>     
  {/strip}
  {/foreach}

我不确定如何实现第二个 foreach 循环。有人有什么建议吗?我对文档有点困惑,因为似乎有两种嵌套 foreach 循环的方法,其中一种似乎已被弃用。 foreach 循环是最好的方法吗?或者 smarty 中还有其他推荐的方法吗?任何反馈将不胜感激。谢谢!

I'm trying to print a subarray in an array with the smarty template engine. Say I have an array structure like this:

Array
    (
[0] => Array
    (
        [directory] => One Day As A Lion - 2011-03-01/
        [source_notes] => iRiver
        [recording_type] => Audience
        [type] => Audio
        [source_type] => MP3
        [fileList] => Array
            (
                [0] => 09 One Day As A Lion.mp3
                [1] => 01 Intro.mp3
                [2] => 05 Ocean View.mp3
                [3] => 04 Swashbuckler.mp3
                [4] => One Day As A Lion - 2011-03-01 - Prince Bandroom, Melbourne, Australia.jpg
                [5] => 10 Peart Plus.mp3
                [6] => 06 Rockers.mp3
                [7] => 03 Last Letter.mp3
                [8] => 07 Swampy.mp3
                [9] => 02 If You Fear Dying.mp3
                [10] => 08 Wild International.mp3
            )

    )

)

How exactly would I get the array containing the filenames to print in smarty? Currently I have a foreach loop in smarty that looks like:

  {foreach $sources as $sourceInfo}
  {strip}       
    Recording Type: {$sourceInfo.type} : {$sourceInfo.recording_type}<br>
    Source : {$sourceInfo.source_notes}<br>     
  {/strip}
  {/foreach}

And I'm not sure how to implement a second foreach loop. Does anyone have any suggestions? I'm a little confused with the documentation since there seems to be two methods of nested foreach loops, one of which seems to be deprecated. Is a foreach loop the best way to do it, or is there another recommended way in smarty? Any feedback would be much appreciated. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天涯离梦残月幽梦 2024-11-17 12:08:32

只需添加另一个 foreach:

{foreach from=$sources item=sourceInfo}
  {strip}       
    Recording Type: {$sourceInfo.type} : {$sourceInfo.recording_type}<br>
    Source : {$sourceInfo.source_notes}<br>     
    Files: {foreach from=$sourceInfo.fileList item=file}{$file}, {foreachelse}<i>no files</i>{/foreach}
  {/strip}
{/foreach}

Simply add another foreach:

{foreach from=$sources item=sourceInfo}
  {strip}       
    Recording Type: {$sourceInfo.type} : {$sourceInfo.recording_type}<br>
    Source : {$sourceInfo.source_notes}<br>     
    Files: {foreach from=$sourceInfo.fileList item=file}{$file}, {foreachelse}<i>no files</i>{/foreach}
  {/strip}
{/foreach}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文