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
I found the answer :
{% for key,value in array_path %}
Key : {{ key }}
Value : {{ value }}
{% endfor %}
There's this example in the SensioLab page on the for
tag:
<h1>Members</h1>
<ul>
{% for key, user in users %}
<li>{{ key }}: {{ user.username|e }}</li>
{% endfor %}
</ul>
http://twig.sensiolabs.org/doc/tags/for.html#iterating-over-keys
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 %}
<li>{{ key }}: {{ user.username|e }}</li>
{% endfor %}
It never hurts to search before asking :)