如何使用twig从一个或多维数组中拉出一个值

时间:2015-09-22 14:38:14

标签: arrays symfony multidimensional-array twig

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

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

我知道如何使用标准PHP横向数据。但是,我不知道如何使用TWIG执行此操作。我只想在图片标签中显示low_resolution网址。我得到的壁橱如下:

{% 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
                )

        )

1 个答案:

答案 0 :(得分:0)

替换它:

    {% 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