falinsky falinsky - 8 months ago 91
PHP Question

Setting element of array from Twig

How can I set member of an already existing array from Twig?

I tried doing it next way:

{% set arr['element'] = 'value' %}

but I got the following error:

Unexpected token "punctuation" of value "[" ("end of statement block"
expected) in ...


There is no nice way to do this in Twig. It is, however, possible by using the merge filter:

{% set arr = arr|merge({'element': 'value'}) %}