可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I use Twig and I have an array with key like this :
array[1] = "alpha" array[2] = "bravo" array[3] = "charlie" array[8] = "delta" array[9] = "echo"
And I would like to get the key (1,2,3,8,9)
and the content (alpha, bravo, charlie, delta, echo)
in a loop to get all value of this array.
How I can do this ?
Thank you
回答1:
I found the answer :
{% for key,value in array_path %} Key : {{ key }} Value : {{ value }} {% endfor %}
回答2:
There's this example in the SensioLab page on the for
tag:
Members
{% for key, user in users %} - {{ key }}: {{ user.username|e }}
{% endfor %}
http://twig.sensiolabs.org/doc/tags/for.html#iterating-over-keys
回答3:
I guess you want to do the "Iterating over Keys and Values"
As the doc here says, just add "|keys" in the variable you want and it will magically happen.
{% for key, user in users %} {{ key }}: {{ user.username|e }} {% endfor %}
It never hurts to search before asking :)