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 = {} %}23{% 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] }) %}78 {# 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 }) %}1314 {% endif %}15{% endfor %}1617{{ dump(locations) }}
Source:
autolink is missing