Ramita para lazo y matriz con llave
Uso Ramita y tengo una matriz con clave como esta:
array[1] = "alpha"
array[2] = "bravo"
array[3] = "charlie"
array[8] = "delta"
array[9] = "echo"
Y me gustaría obtener la clave (1,2,3,8,9)
y el contenido (alpha, bravo, charlie, delta, echo)
en un bucle para obtener todo el valor de esta matriz.
¿Cómo puedo hacer esto ?
Gracias
124
3 answers
Encontré la respuesta:
{% 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/ajaxhispano.com/template/agent.layouts/content.php on line 61
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/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-04-24 14:00:33
Hay este ejemplo en la página de SensioLab en la etiqueta 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/ajaxhispano.com/template/agent.layouts/content.php on line 61
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/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-11-13 15:24:55
Supongo que quieres hacer la"Iteración sobre Claves y valores"
Como dice el doc aquí, simplemente agregue "|keys" en la variable que desea y ocurrirá mágicamente.
{% for key, user in users %}
<li>{{ key }}: {{ user.username|e }}</li>
{% endfor %}
Nunca está de más buscar antes de preguntar:)
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/ajaxhispano.com/template/agent.layouts/content.php on line 61
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/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-10-29 04:57:02