Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twig for loop for arrays with keys

I use Twig and I have an array with keys 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 do I solve this problem?

like image 929
Guillaume Avatar asked Apr 24 '12 13:04

Guillaume


People also ask

What is Loop index0?

loop.index0. The current iteration of the loop. ( 0 indexed) loop.revindex. The number of iterations from the end of the loop (1 indexed)


2 Answers

I found the answer :

{% for key,value in array_path %}     Key : {{ key }}     Value : {{ value }} {% endfor %} 
like image 200
Guillaume Avatar answered Sep 28 '22 08:09

Guillaume


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

like image 38
dkinzer Avatar answered Sep 28 '22 07:09

dkinzer