Gałązka do pętli i tablicy z kluczem
Używam gałązki i mam tablicę z kluczem w ten sposób:
array[1] = "alpha"
array[2] = "bravo"
array[3] = "charlie"
array[8] = "delta"
array[9] = "echo"
I chciałbym uzyskać klucz (1,2,3,8,9)
i zawartość (alpha, bravo, charlie, delta, echo)
W pętli, aby uzyskać całą wartość tej tablicy.
Dziękuję
124
3 answers
Znalazłem odpowiedź:
{% for key,value in array_path %}
Key : {{ key }}
Value : {{ value }}
{% endfor %}
270
Author: Guillaume,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-04-24 14:00:33
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-04-24 14:00:33
Jest taki przykład na stronie SensioLab o tagu for
:
<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
43
Author: dkinzer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-11-13 15:24:55
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-11-13 15:24:55
Myślę, że chcesz zrobić "iterację nad kluczami i wartościami"
Jak mówi doc tutaj, po prostu dodaj" / keys " w zmiennej, którą chcesz, a stanie się to w magiczny sposób.
{% for key, user in users %}
<li>{{ key }}: {{ user.username|e }}</li>
{% endfor %}
Nigdy nie zaszkodzi szukać przed pytaniem:)
1
Author: imcoddy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-29 04:57:02
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-29 04:57:02