1

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'].

2

This should do it for you:

{% set myArray = {
    myKey: [
        'value1',
    ]
} %}

{% set myArray = myArray|merge({myKey: myArray.myKey | merge(['value2']) }) %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.