Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-12T22:10:41.747Z
Answers.Text: Of course! I had the feeling it was something obvious, but just not to me - thanks, Paul, will give that a try
Answers.UserDisplayName: Roi Agneta
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-12T19:31:49.783Z
Answers.LastActivityDate: 2018-04-12T19:31:49.783Z
Answers.UserDisplayName: Paul
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-12T22:47:32.123Z
Answers.LastActivityDate: 2018-04-12T22:47:32.123Z
Answers.UserDisplayName: Roi Agneta
Body: <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>
  
Tags: <merge>
Title: Using Merge filter
ThreadText: # 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 %}
    <div>{{ item }}</div>
  {% 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, username_1, will give that a try  
  
  ---
  
  ### Answer by username_0 on 2018-04-12. Score: 0
  
  The problem, as noted by username_1 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 #}
          <div>{{item.eventDate}} {{item.eventTitle}}</div>
      {% 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](https://craftcms.stackexchange.com/questions/14255/create-an-object-of-objects-with-merge?rq=1)
  
  
  ---
  
  