<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>

CC BY-SA 4.0
2020-04-17T11:11:36.057Z
2020-04-17T11:11:36.057Z
Oli
<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>

<templating><craft3><array>
Pushing additional key values without overwriting existing key values in an array
# 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']) }) %}


---

