Filter / Organize array into key, value pairs for twig

Some twig stuff that I found to organize into key, value pairs.

January 17, 2022

php

1{% set locations = {} %}
2
3{% for location in locationList.locations.all() %}
4 {# create a dynamic property if one doesn't exist #}
5 {% if not attribute(locations, location.locationState) is defined %}
6 {% set locations = locations|merge({ (location.locationState) : [location] }) %}
7
8 {# else update the dynamic property with the new title #}
9 {% else %}
10 {% set arr = attribute(locations, location.locationState) %}
11 {% set arr = arr|merge([location]) %}
12 {% set locations = locations|merge({ (location.locationState) : arr }) %}
13
14 {% endif %}
15{% endfor %}
16
17{{ dump(locations) }}

Source: 

autolink is missing

Invely's