2 回答

TA貢獻1851條經驗 獲得超3個贊
您可以遍歷代碼數組:
{% for c in code %}
{# ... #}
{% endfor %}
文檔: https : //twig.symfony.com/doc/2.x/tags/for.html
然后,如果項目確實匹配:
{# ... #}
{% if airport == c %}
{# ... #}
{% endif %}
{# ... #}
文檔: https : //twig.symfony.com/doc/2.x/tags/if.html
在相同的循環索引處替換變量 airport:
{# ... #}
{% set airport = city[loop.index0] %}
{# ... #}
文檔: https : //twig.symfony.com/doc/2.x/tags/for.html#the-loop-variable
所以,完整的:
{% for c in code %}
{% if airport == c %}
{% set airport = city[loop. index0] %}
{% endif %}
{% endfor %}
運行小提琴:https : //twigfiddle.com/xflfas/2
超出范圍說明:您的數組最好命名為cities和codes。
這樣,當你遍歷它們時,你最終會得到有意義的命名
{% set codes = ['AMS', 'EIN', 'RTM'] %}
{% for code in codes %}
{{ code }}
{% endfor %}
{# and #}
{% set cities = ['Amsterdam', 'Eindhoven', 'Rotterdam'] %}
{% for city in cities %}
{{ city }}
{% endfor %}

TA貢獻1856條經驗 獲得超11個贊
用于{% if value in array %}
從第一個數組和 Twig 的合并函數中搜索以替換第二個數組中的值。請參閱此https://twig.symfony.com/doc/2.x/filters/merge.html
- 2 回答
- 0 關注
- 188 瀏覽
添加回答
舉報