<p>You have a couple of errors in your code. First,</p>
<p><code>{{ set = transforms: [{ width: 2000, height: 1333 }] }}</code></p>
<p>Should be:</p>
<p><code>{% set transforms = [{ width: 2000, height: 1333 }] %}</code></p>
<p>And inside you for loop, this:</p>
<p><code>{{ transforms_WEBP[loop.index0]|merge({format: 'webp'}) }}</code></p>
<p>Should be something more like this:</p>
<p><code>{% set transforms_WEBP = transforms_WEBP[loop.index0]|merge({format: 'webp'}) %}</code></p>
<p><strong>HOWEVER...</strong></p>
<p>That's also not going to work, because you can't directly set the index value of a twig array like that, the way you can in PHP.</p>
<p>What I suggest you do is re-factor your code to something a little closer to this:</p>
<pre><code>    {% set transforms = [
        { width: 2000, height: 1333 },
        { width: 1000, height: 667 },
    ] %}

    {% for i in transforms %}           
        {% set i = i|merge({format: 'webp'}) %}
        {% set transforms = transforms|merge([i]) %}
    {% endfor %}
</code></pre>
<p>This will result in a <code>transforms</code> variable that looks like this:</p>
<pre><code>[
    0 =&gt; [
        'width' =&gt; 2000
        'height' =&gt; 1333
    ]
    1 =&gt; [
        'width' =&gt; 1000
        'height' =&gt; 667
    ]
    2 =&gt; [
        'width' =&gt; 2000
        'height' =&gt; 1333
        'format' =&gt; 'webp'
    ]
    3 =&gt; [
        'width' =&gt; 1000
        'height' =&gt; 667
        'format' =&gt; 'webp'
    ]
]
</code></pre>

CC BY-SA 4.0
2022-09-24T20:52:37.307Z
2022-09-24T20:52:37.307Z
stevecomrie
<p>I am looping over an array of objects and trying to add a new Key and Value.</p>
<p>I keep getting a host of errors so was hoping someone could help a little with formatting this loop.</p>
<pre><code>{{ set = transforms: [{ width: 2000, height: 1333 }] }}

{%- set transforms_WEBP = transforms -%}
{%- set transforms_JPG = transforms|default([]) -%}

{% for i in transforms_WEBP %}
  {{ transforms_WEBP[loop.index0]|merge({format: 'webp'}) }}
{% endfor %}

{% set transforms = transforms_WEBP|merge(transforms_JPG) %}
</code></pre>
<p>I keep getting ARRAY to String conversion but I am missing where I am going wrong.</p>
<p>Thanks for any help :)</p>

<forloop><array>
Add an Object key and value to an exsiting object
# Add an Object key and value to an exsiting object

(Asked by: username_0 on 2022-09-24)

I am looping over an array of objects and trying to add a new Key and Value.
I keep getting a host of errors so was hoping someone could help a little with formatting this loop.
{{ set = transforms: [{ width: 2000, height: 1333 }] }}

{%- set transforms_WEBP = transforms -%}
{%- set transforms_JPG = transforms|default([]) -%}

{% for i in transforms_WEBP %}
  {{ transforms_WEBP[loop.index0]|merge({format: 'webp'}) }}
{% endfor %}

{% set transforms = transforms_WEBP|merge(transforms_JPG) %}

I keep getting ARRAY to String conversion but I am missing where I am going wrong.
Thanks for any help :)

**Tags:** forloop, array


---

## 1 answers

### Answer by username_1 on 2022-09-24. Score: 1

You have a couple of errors in your code. First,
{{ set = transforms: [{ width: 2000, height: 1333 }] }}
Should be:
{% set transforms = [{ width: 2000, height: 1333 }] %}
And inside you for loop, this:
{{ transforms_WEBP[loop.index0]|merge({format: 'webp'}) }}
Should be something more like this:
{% set transforms_WEBP = transforms_WEBP[loop.index0]|merge({format: 'webp'}) %}
HOWEVER...
That's also not going to work, because you can't directly set the index value of a twig array like that, the way you can in PHP.
What I suggest you do is re-factor your code to something a little closer to this:
    {% set transforms = [
        { width: 2000, height: 1333 },
        { width: 1000, height: 667 },
    ] %}

    {% for i in transforms %}           
        {% set i = i|merge({format: 'webp'}) %}
        {% set transforms = transforms|merge([i]) %}
    {% endfor %}

This will result in a transforms variable that looks like this:
[
    0 => [
        'width' => 2000
        'height' => 1333
    ]
    1 => [
        'width' => 1000
        'height' => 667
    ]
    2 => [
        'width' => 2000
        'height' => 1333
        'format' => 'webp'
    ]
    3 => [
        'width' => 1000
        'height' => 667
        'format' => 'webp'
    ]
]


---

