Setting element of array from Twig
I ran into this problem but was trying to create integer indexes instead of associative index like 'element'.
You need to protect your index key with ()
using the merge filter as well:
{% set arr = arr|merge({ (loop.index0): 'value'}) %}
You can now add custom index key like ('element'~loop.index0)