简体   繁体   English

使用smarty分配多维数组

[英]Using smarty to assign multidimensional array

I have the following xml which I'm accessing through simplexml: 我有以下要通过simplexml访问的xml:

<sequences>
    <sequence>
        <ImageUrl id="">
            http://www.image.com/image.jpg
        </ImageUrl>
        <photographer>name</photographer>
    </sequence>
    <sequence>
        <ImageUrl id="">
            http://www.image.com/image1.jpg
        </ImageUrl>
        <photographer>name 1</photographer>
    </sequence>
</sequences>

I need to pass this data to smarty and output in a template. 我需要将此数据传递给smarty并在模板中输出。 I need to be able to output the first sequence image and photographer name and then the second. 我需要能够输出第一个序列图像和摄影师名称,然后输出第二个。 How can I do this? 我怎样才能做到这一点? I can see that you can pass an array to smarty and then loop over it in the template but I essentially need to pass a multidimensional array, 1 array for each sequence node. 我可以看到可以将一个数组传递给smarty,然后在模板中对其进行循环,但是我基本上需要传递一个多维数组,每个序列节点一个数组。

I ended up assigning the whole simplexml obj to a smarty and looped over it like to in the template: 我最终将整个simplexml obj分配给一个smarty,并像在模板中那样遍历了它:

{foreach from=$contents key=key item=item}
    {foreach from=$item key=k item=i}
        {if $k eq 'ImageUrl'}
            <img src="{$i}" />
        {/if}
        {if $k eq 'photographer'}
            <img src="{$i}" />
        {/if}

Convert your XML to array then : 然后将XML转换为数组:

In php 在PHP中

<?php
$arr = array("first"=>1000, "second"=>1001, "third"=>1002);
$smarty->assign('myArray', $arr);
?>

In smarty template : 在聪明的模板中:

<ul>
{foreach from=$myArray item=myArray name=myArray}
    <li>{$myArray.first}</li>
    <li>{$myArray.second}</li>
    <li>{$myArray.third}</li>
{/foreach}
</ul>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM