Twig for loop and array with key

匿名 (未验证) 提交于 2019-12-03 02:14:01

问题:

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 :)



    标签
    易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
    该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!