简体   繁体   中英

Multidimensional Array on TWIG loop

I'm trying to save some values into a multiarray on Twig, but for some reason, it's not working.

I want to save in an array a list of users and some values of them. I am using merge function to create the multi array.

{% for result in results %}
        {% set users = users|merge({ ('name'~loop.index):result.name,('age'~loop.index):result.age,('credits'~loop.index):result.credits}) %}
{% endfor %}

Try this out :

{% set users = [] %}
{% for result in results %}
    {% set users = users|merge([{ 'name' : result.name, 'age' : result.age, 'credits' : result.credits }]) %}
{% endfor %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM