Creating dynamic array and set keys I have content which needs to go into a certain column so I have a field called 'column' there. I can create the array I want by hand:

{%
    set items = {
        1 : {
            0 : 'Quick Facts',
            1 : 'Agencies'
        },
        2 : {
            0 : 'Inspiration',
            1 : 'Industry Experience'
        }
    }
%}


Where 1 and 2 are the columns, and 0,1 is just a normal index. In a loop it is different. I have tried the same syntax but of course setting the variable in the loop will set the new variable every time and replace the old one. Using merge like below always gives me new keys. Is there a way to do this as easily as PHP?

{% set blocks = {} %}

{% for tab in tabs %}
    {% for key,block in tab.tabItem %}

        {% if block.type == 'about' %}
            {% set blocks = blocks|merge({ (block.column.value) : block.aboutTitle}) %}
        {% endif %}

    {% endfor %}
{% endfor %}


This is the output of the two pieces of code (first being what I want). I'd like the block.column.value to be the key.
You can do this using the twig attribute function:

{% set data = [
    {'column':'1','title':'Quick Facts'}, 
    {'column':'2','title':'Inspiration'}, 
    {'column':'1','title':'Agencies'}, 
    {'column':'2','title':'Industry Experience'}
] %}

{% set cols = {} %}
{% for item in data %}

    {# create a dynamic property if one doesn't exist #}
    {% if not attribute(cols, 'col' ~ item.column) is defined %}
        {% set cols = cols|merge({ ('col' ~ item.column) : [item.title] }) %}

    {# else update the dynamic property with the new title #}
    {% else %}
        {% set titles = attribute(cols, 'col' ~ item.column) %}
        {% set titles = titles|merge([item.title]) %}
        {% set cols = cols|merge({ ('col' ~ item.column) : titles }) %}

    {% endif %}
{% endfor %}

{{ dump(cols) }}

{#
array(2) { 
    ["col1"]=&gt; array(2) {
         [0]=&gt; string(11) "Quick Facts" 
         [1]=&gt; string(8) "Agencies" 
    } 
    ["col2"]=&gt; array(2) { 
        [0]=&gt; string(11) "Inspiration" 
        [1]=&gt; string(19) "Industry Experience" 
    } 
} 
#}