Twig for loop for arrays with keys

To loop through an array with keys in Twig, you can use the for loop and access the key and value of each element in the array using the loop variable. For example:

{% for key, value in array %}
    Key: {{ key }} Value: {{ value }}
{% endfor %}

Watch a course Learn object oriented PHP

You can also use the key and value variables to access the current key and value within the loop. For example:

{% for key, value in array %}
    Key: {{ key }} Value: {{ value }}
    {# do something with key and value #}
{% endfor %}

You can also use the keys and values filters to get the keys and values of an array separately:

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

{% for value in array|values %}
    Value: {{ value }}
{% endfor %}