thomascirca
thomascirca

Reputation: 933

Printing a subarray in an array in smarty (foreach)

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!

Upvotes: 1

Views: 4898

Answers (1)

Czechnology
Czechnology

Reputation: 14992

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}

Upvotes: 4

Related Questions