Answers.Body: <p>This should do it for you:</p>
  
  <pre><code>{% set myArray = {
      myKey: [
          'value1',
      ]
  } %}
  
  {% set myArray = myArray|merge({myKey: myArray.myKey | merge(['value2']) }) %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-04-17T11:11:36.057Z
Answers.LastActivityDate: 2020-04-17T11:11:36.057Z
Answers.UserDisplayName: Oli
Body: <p>For an existing key in an associative array, I'm trying to push a new value to it without overwriting the existing values in the key. I've tried this:</p>
  
  <pre><code>{% set myArray = {
      myKey: [
          'value1',
      ]
  } %}
  {% set myArray = myArray|merge({key: 'value2'}) %} 
  </code></pre>
  
  <p>But this returns <code>[ 'myKey' : 'value2']</code>. I'm trying to get it to return <code>[ 'myKey' : [ 0 =&gt; 'value1', 1 =&gt; 'value2']</code>.</p>
  
Tags: <templating><craft3><array>
Title: Pushing additional key values without overwriting existing key values in an array
ThreadText: # Pushing additional key values without overwriting existing key values in an array
  
  (Asked by: username_0 on 2020-04-16)
  
  For an existing key in an associative array, I'm trying to push a new value to it without overwriting the existing values in the key. I've tried this:
  
  ```
  {% set myArray = {
      myKey: [
          'value1',
      ]
  } %}
  {% set myArray = myArray|merge({key: 'value2'}) %} 
  
  ```
  
  But this returns `[ 'myKey' : 'value2']`. I'm trying to get it to return `[ 'myKey' : [ 0 => 'value1', 1 => 'value2']`.
  
  **Tags:** templating, craft3, array
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-04-17. Score: 2 | Accepted answer
  
  This should do it for you:
  
  ```
  {% set myArray = {
      myKey: [
          'value1',
      ]
  } %}
  
  {% set myArray = myArray|merge({myKey: myArray.myKey | merge(['value2']) }) %}
  
  ```
  
  
  ---
  
  