Setting element of array from Twig

前端 未结 10 769
北恋
北恋 2020-12-04 09:55

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

I tried doing it next way:

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

but

10条回答
  •  渐次进展
    2020-12-04 10:24

    I have tried @LivaX 's answer but it does not work , merging an array where keys are numeric wont work ( https://github.com/twigphp/Twig/issues/789 ).

    That will work only when keys are strings

    What I did is recreate another table ( temp) from the initial table (t) and make the keys a string , for example :

    {% for key , value in t%}
    {% set temp= temp|merge({(key~'_'):value}) %}
    {% endfor %}
    

    t keys : 0 , 1 , 2 ..

    temp keys : 0_, 1_ , 2_ ....

提交回复
热议问题