Smarty循环嵌套数组

时间:2012-03-19 12:18:23

标签: php foreach smarty

您好我想打印嵌套数组的数据,这是我的数组:$ result []

结果数组就像这样构建

foreach($pictureIds as $pressId) {
    $picture = PressPicture::_getItemByPressId($pressId);
    $result['picture'][] = $picture;
    $pressItem = PressItem::_fromNumber($pressId);

    if($pressItem) {
        $result['title'][] = $pressItem->getTitle();
    }
}

Array
    (
    [picture] => Array
        (
            [0] => PressPicture Object
                (
                    [id] => 21
                    [data] => Array
                        (
                            [id] => 21
                            [press_id] => 3
                            [update_time] => 1331738139
                            [ord] => 1
                        )

                    [dataLang] => 
                    [prettyClassName] => 
                )

            [1] => PressPicture Object
                (
                    [id] => 31
                    [data] => Array
                        (
                            [id] => 31
                            [press_id] => 4
                            [update_time] => 1332144196
                            [ord] => 1
                        )

                    [dataLang] => 
                    [prettyClassName] => 
                )

        )

    [title] => Array
        (
            [0] => Tetsij
            [1] => Persbericht
        )
)

如何循环遍历此数组。所以我有我的头衔,然后是我的照片。我试过这个

{foreach name=outer item=it from=$result}
<li>
    {foreach from=$it item=value key=key}
      {assign var=item value=$value}
      <img src="{$item->getPictureUrl('list', 180, 120, true, false, true)}" alt="{$item->getTitle()}" width="180" height="120" />
     {/foreach}
</li>
{/foreach}

我可以使用该对象但是如何打印同样位于数组中的标题?

1 个答案:

答案 0 :(得分:0)

使用图片foreach中的key参数作为索引来访问标题数组。

{foreach name=outer item=pictures from=$result.picture}
<li>
    {foreach from=$pictures item=pic key=key}
        <img src="{$pic->getPictureUrl('list', 180, 120, true, false, true)}" alt="{$result.title[$key]}" width="180" height="120" />
    {/foreach}
</li>
{/foreach}