如何拉出一个值或多维数组使用树枝



我正在做一个学习symfony2/twig的测试项目。我所要做的就是显示一个由位置决定的图像列表。

我正在从Instagram的API检索这些图像的数据。API发送回JSON数据。每张图片都有丰富的信息。我只需要将low_resolution值放到image标签中。

我知道如何横向数据使用标准PHP。然而,我不知道如何使用TWIG做到这一点。我只想在图像标记中显示低分辨率url。我得到的衣橱是这样的:

{% if images_array == true %}
        {% for key,value in images_array %}
            {{ value.link }}
            {% for key,value in value.images %}
                <div class="col-md-3">
                    <img src="{{ value.url }}" alt="" class="thumbnail img-responsive" />                   
                </div>
            {% endfor %}
        {% endfor %}
    {% endif %}

上面的问题是,它循环遍历[images]中的所有[url],而不是显示[low_resolution]

中的值

下面是从Instagram引入的数据的第一个索引。

Array
(
    [0] => Array
        (
            [attribution] => 
            [tags] => Array
                (
                    [0] => dubai
                )
            [location] => Array
                (
                    [latitude] => -29.854119007
                    [name] => ICC Durban Exhibition Centre,Durban,South Africa
                    [longitude] => 31.029556697
                    [id] => 450025614
                )
            [comments] => Array
                (
                    [count] => 0
                    [data] => Array
                        (
                        )
                )
            [filter] => Normal
            [created_time] => 1442931165
            [link] => https://instagram.com/p/775BhjE1kn/
            [likes] => Array
                (
                    [count] => 0
                    [data] => Array
                        (
                        )
                )
            [images] => Array
                (
                    [low_resolution] => Array
                        (
                            [url] => https://scontent.cdninstagram.com/hphotos-xaf1/t51.2885-15/s320x320/e35/11887246_500339553458410_480642133_n.jpg
                            [width] => 320
                            [height] => 320
                        )
                    [thumbnail] => Array
                        (
                            [url] => https://scontent.cdninstagram.com/hphotos-xaf1/t51.2885-15/s150x150/e35/11887246_500339553458410_480642133_n.jpg
                            [width] => 150
                            [height] => 150
                        )
                    [standard_resolution] => Array
                        (
                            [url] => https://scontent.cdninstagram.com/hphotos-xaf1/t51.2885-15/s640x640/sh0.08/e35/11887246_500339553458410_480642133_n.jpg
                            [width] => 640
                            [height] => 640
                        )
                )
            [users_in_photo] => Array
                (
                )
            [caption] => Array
                (
                    [created_time] => 1442931165
                    [text] => Crecimiento importante a nivel Turistico y aeroportuario en #Dubai
                    [from] => Array
                        (
                            [username] => joseantonio1977
                            [profile_picture] => https://scontent.cdninstagram.com/hphotos-xfa1/t51.2885-19/11261324_916468171759729_1477908664_a.jpg
                            [id] => 194601237
                            [full_name] => Jose Antonio Lopez Sosa
                        )
                    [id] => 1079707331921664292
                )
            [type] => image
            [id] => 1079707329077926183_194601237
            [user] => Array
                (
                    [username] => joseantonio1977
                    [profile_picture] => https://scontent.cdninstagram.com/hphotos-xfa1/t51.2885-19/11261324_916468171759729_1477908664_a.jpg
                    [id] => 194601237
                    [full_name] => Jose Antonio Lopez Sosa
                )
        )

替换为

    {% for key,value in value.images %}
        <div class="col-md-3">
            <img src="{{ value.url }}" alt="" class="thumbnail img-responsive" />                   
        </div>
    {% endfor %}
由:

    <div class="col-md-3">
        <img src="{{ value.images.low_resolution.url }}" alt="" class="thumbnail img-responsive" />                 
    </div>

确保不要在多个循环中使用两次keyvalue变量

根据另一个stackoverflow答案,下面是如何在TWIG中导航关联数组:https://stackoverflow.com/a/14199125/684101

相关内容

  • 没有找到相关文章

最新更新