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?
loop.index0. The current iteration of the loop. ( 0 indexed) loop.revindex. The number of iterations from the end of the loop (1 indexed)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With