如何使用树枝从一个或多维数组中提取一个值
How to pull a value out a or multi dimensional array using twig
我正在做一个测试项目来学习symfony2/twig。我想要做的就是显示一个由位置决定的图像列表。
我正在从 Instagram API 中检索这些图像的数据。 API 发回 JSON 数据。每张图片都有丰富的信息。我只需要将 low_resolution 值放在图像标签中。
我知道如何使用标准 PHP 横向处理数据。但是,我不知道如何使用 TWIG 执行此操作。我只想在图片标签中显示 low_resolution 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>
确保不要在多个循环中使用两次 key
和 value
变量
基于另一个 Whosebug 答案,以下是如何在 TWIG 中的关联数组中导航:
我正在做一个测试项目来学习symfony2/twig。我想要做的就是显示一个由位置决定的图像列表。
我正在从 Instagram API 中检索这些图像的数据。 API 发回 JSON 数据。每张图片都有丰富的信息。我只需要将 low_resolution 值放在图像标签中。
我知道如何使用标准 PHP 横向处理数据。但是,我不知道如何使用 TWIG 执行此操作。我只想在图片标签中显示 low_resolution 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>
确保不要在多个循环中使用两次 key
和 value
变量
基于另一个 Whosebug 答案,以下是如何在 TWIG 中的关联数组中导航: