<p>What your merge filter does now, is merge the new object with the previous one, thus overriding the values for both keys.</p>

<p>Try this:</p>

<pre><code>{% set allWorkshops = allWorkshops|merge([{ 
    'eventDate' : event.startDate,
    'name' : entry.title                    
}]) %}
</code></pre>

<p>Merging your existing array with a new array containing one object.</p>

CC BY-SA 3.0
2018-04-12T22:10:41.747Z
Of course! I had the feeling it was something obvious, but just not to me - thanks, Paul, will give that a try
Roi Agneta
CC BY-SA 3.0
2018-04-12T19:31:49.783Z
2018-04-12T19:31:49.783Z
Paul
<p>The problem, as noted by Paul Verheul, is that the key:values created in each loop overwrites the previous one. The solution is to save each iteration as an object in a new array. The code now looks like this:</p>

<pre><code>{% set allWorkshops = [] %} 
{% set workshop = [] %}

{% set workshops = craft.entries.section('workshop') %} {# get entries #}   

    {% for entry in workshops %}

                {% set event = craft.venti.getEventById(entry.id) %}    {# get the Venti array for each entry #}    

                {% set workshop = {
                        'eventDate' : event.startDate,
                        'eventTitle' : entry.title                  
                    } %}

                {% set allWorkshops = allWorkshops|merge([ workshop ]) %}

    {% endfor %}

    {% for item in allWorkshops|sort %}  {# sort items by eventDate #}
        &lt;div&gt;{{item.eventDate}} {{item.eventTitle}}&lt;/div&gt;
    {% endfor %}
</code></pre>

<p>I also came across this answer which was in response to a different question but same basic issue: <a href="https://craftcms.stackexchange.com/questions/14255/create-an-object-of-objects-with-merge?rq=1">Create an object of objects with merge</a></p>

CC BY-SA 3.0
2018-04-12T22:47:32.123Z
2018-04-12T22:47:32.123Z
Roi Agneta
<p>I am trying to use some Twig code to sort entries by a date field that is contained within a Venti field. Here is the code:</p>

<pre><code>{% set allWorkshops = [] %}
{% for entry in craft.entries.section('workshop') %}

            {% set event = craft.venti.getEventById(entry.id) %}    {# get the Venti array for each entry #}    

            {% set eventDate = event.startDate %}  {# get the Venti date field #}


            {% set allWorkshops = allWorkshops|merge({ 
                    'eventDate' : event.startDate,
                    'name' : entry.title                    
                }) %}                               {# merge combined date and title into array #}

{% endfor %}

{% for item in allWorkshops|sort %}
  &lt;div&gt;{{ item }}&lt;/div&gt;
{% endfor %}
</code></pre>

<p>It works, except that the array 'allWorkshops' only contains the last item from the "entry" loop. Obviously 'allWorkshops' is getting overwritten for each iteration of the loop, but I do not understand why, given that it is defined outside the loop.</p>

<p>Thanks for any enlightenment!</p>

<merge>
Using Merge filter
# Using Merge filter

(Asked by: username_0 on 2018-04-12)

I am trying to use some Twig code to sort entries by a date field that is contained within a Venti field. Here is the code:
{% set allWorkshops = [] %}
{% for entry in craft.entries.section('workshop') %}

            {% set event = craft.venti.getEventById(entry.id) %}    {# get the Venti array for each entry #}    

            {% set eventDate = event.startDate %}  {# get the Venti date field #}


            {% set allWorkshops = allWorkshops|merge({ 
                    'eventDate' : event.startDate,
                    'name' : entry.title                    
                }) %}                               {# merge combined date and title into array #}

{% endfor %}

{% for item in allWorkshops|sort %}
  {{ item }}
{% endfor %}

It works, except that the array 'allWorkshops' only contains the last item from the "entry" loop. Obviously 'allWorkshops' is getting overwritten for each iteration of the loop, but I do not understand why, given that it is defined outside the loop.
Thanks for any enlightenment!

**Tags:** merge

---

## 2 answers

### Answer by username_1 on 2018-04-12. Score: 2

What your merge filter does now, is merge the new object with the previous one, thus overriding the values for both keys.
Try this:
{% set allWorkshops = allWorkshops|merge([{ 
    'eventDate' : event.startDate,
    'name' : entry.title                    
}]) %}

Merging your existing array with a new array containing one object.

> **Comments:**
> **username_0**: Of course! I had the feeling it was something obvious, but just not to me - thanks, Paul, will give that a try  

---

### Answer by username_0 on 2018-04-12. Score: 0

The problem, as noted by Paul Verheul, is that the key:values created in each loop overwrites the previous one. The solution is to save each iteration as an object in a new array. The code now looks like this:
{% set allWorkshops = [] %} 
{% set workshop = [] %}

{% set workshops = craft.entries.section('workshop') %} {# get entries #}   

    {% for entry in workshops %}

                {% set event = craft.venti.getEventById(entry.id) %}    {# get the Venti array for each entry #}    

                {% set workshop = {
                        'eventDate' : event.startDate,
                        'eventTitle' : entry.title                  
                    } %}

                {% set allWorkshops = allWorkshops|merge([ workshop ]) %}

    {% endfor %}

    {% for item in allWorkshops|sort %}  {# sort items by eventDate #}
        {{item.eventDate}} {{item.eventTitle}}
    {% endfor %}

I also came across this answer which was in response to a different question but same basic issue: Create an object of objects with merge


---

