Skip to content
Advertisement

Getting array keys in Twig? (Symfony)

Is it possible to get the key of an array in Twig (in Symfony)?

For example if I have an array of:

array(
'key1' => 'value1',
'key2' => 'value2',
);

Is it possible in Twig to print:

key1: value1

key2: value2

Thanks

Advertisement

Answer

Try following format:

{% for key, value in array %}
    {{ key }} - {{ value }}
{% endfor %}

More Information on Offical Twig about Iterating over Keys and Values

https://twig.symfony.com/doc/3.x/tags/for.html#iterating-over-keys-and-values

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement