Getting array keys in Twig? (Symfony) Getting array keys in Twig? (Symfony) symfony symfony

Getting array keys in Twig? (Symfony)


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


You can use the keys filter. The keys filter returns the keys of an array.

{% set keys = array|keys %}

or

{% for key in array|keys %}   {{ key }}{% endfor %}


If you have this array: person = ['name': 'John', 'age': '30'], you can display the value of a specific key, like this:

<p> Name = {{ person.name}} </p><p> Age= {{ person.age}} </p>