<p><em>Note: I have used the English singular <strong>allergen</strong> and plural <strong>allergens</strong> in my examples here—it’s just easier for me to proofread in my native language!</em></p>
<p>I believe what is happening is that your code is repeatedly merging an associative array on top of an associative array, overwriting the same <code>allergen</code> key.</p>
<p>That said,<code>recipe.allergens</code> is probably already an array—can you just use it as-is? It would be helpful to know what kind of field you're working with.</p>
<p>To rebuild the array, you would need append to <code>allergensArray</code>, like this:</p>
<pre><code>{% set allergensArray = [] %}

{% for allergen in recipe.allergens %}
  {# Create a single-element array to merge: #}
  {% set allergensArray = allergensArray|merge([allergen]) %}
{% endfor %}

{{ dump(allergensArray) }}
</code></pre>
<p>The core issue (I think) is that the curly braces inside your <code>merge()</code> filter (<code>merge({ allergen })</code>) is creating an object like this…</p>
<pre><code>{
  allergen: (Allergen model 1)
}
</code></pre>
<p>…which is then getting merged with another object like this, the second time around…</p>
<pre><code>{
  allergen: (Allergen model 2)
}
</code></pre>
<p>…and squashing the <code>allergen</code> key from the first loop.</p>
<hr />
<p>Aside: I don't recognize <code>App\Enti…</code> as a Craft namespace—is this indeed a Craft application?</p>

CC BY-SA 4.0
2023-08-25T16:23:54.083Z
2023-08-25T16:23:54.083Z
August Miller
<p>Thank you very much for your response which is very clear :</p>
<p>Yes, the <code>recipe.allergens</code> is an array which contains <code>alergene</code> types with an <code>id</code> and a <code>name</code> properties.</p>
<p>-&gt; In DB : <code>Allergen(id, name)</code>.</p>
<p>I tried your solution but it doesn't work. I don't think it is the good way to do...</p>
<pre><code>{% set allergensArray = [] %}
    {% for allergen in recipe.allergenes %}
      {% set singleElement = {'id' : allergen.id, 'name' : allergen.name} %}
      {% set allergensArray = allergensArray|merge({singleElement}) %}
    {% endfor %}
    {{ dump(allergensArray) }}
</code></pre>
<p>The result is :
<a href="https://i.sstatic.net/Phx8f.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Phx8f.png" alt="Result" /></a></p>
<p>I am googling for a solution but not found for the moment.</p>
<p>Thank you again</p>

CC BY-SA 4.0
2023-08-28T01:17:43.460Z
Inside your merge, you need to wrap the singleElement with square bracket instead curly bracket, so it would allergensArray|merge([singleElement])
aodihis
CC BY-SA 4.0
2023-08-25T22:08:58.947Z
2023-08-26T08:17:27.413Z
2023-08-26T08:17:27.413Z
Nicolas
<p>Sorry per advance for my english :/</p>
<p>I am trying to construct an twig array in a .twig file like this :</p>
<pre><code>{% set allergenesArray = [] %}
    {% for allergne in recipe.allergenes %}
      {% set allergenesArray = allergenesArray|merge({
        allergne
      }) %}
    {% endfor %}
    {{ dump(allergenesArray) }}
</code></pre>
<p>The result of the dump is <a href="https://i.sstatic.net/lLbU5.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/lLbU5.png" alt="Dump of the array" /></a>.</p>
<p>The code works fine but in fact, I should have <strong>2</strong> objects : I think the first one is erased but I really don't why...</p>
<p>If anyone has an idea...</p>
<p>Thanks per advance and good evening</p>

<templating>
Twig > for looping on objects -> only one result
# Twig > for looping on objects -> only one result

(Asked by: username_0 on 2023-08-25)

Sorry per advance for my english :/
I am trying to construct an twig array in a .twig file like this :
{% set allergenesArray = [] %}
    {% for allergne in recipe.allergenes %}
      {% set allergenesArray = allergenesArray|merge({
        allergne
      }) %}
    {% endfor %}
    {{ dump(allergenesArray) }}

The result of the dump is .
The code works fine but in fact, I should have 2 objects : I think the first one is erased but I really don't why...
If anyone has an idea...
Thanks per advance and good evening

**Tags:** templating


---

## 2 answers

### Answer by username_1 on 2023-08-25. Score: 2

Note: I have used the English singular allergen and plural allergens in my examples here—it’s just easier for me to proofread in my native language!
I believe what is happening is that your code is repeatedly merging an associative array on top of an associative array, overwriting the same allergen key.
That said,recipe.allergens is probably already an array—can you just use it as-is? It would be helpful to know what kind of field you're working with.
To rebuild the array, you would need append to allergensArray, like this:
{% set allergensArray = [] %}

{% for allergen in recipe.allergens %}
  {# Create a single-element array to merge: #}
  {% set allergensArray = allergensArray|merge([allergen]) %}
{% endfor %}

{{ dump(allergensArray) }}

The core issue (I think) is that the curly braces inside your merge() filter (merge({ allergen })) is creating an object like this…
{
  allergen: (Allergen model 1)
}

…which is then getting merged with another object like this, the second time around…
{
  allergen: (Allergen model 2)
}

…and squashing the allergen key from the first loop.

Aside: I don't recognize App\Enti… as a Craft namespace—is this indeed a Craft application?


---

### Answer by username_0 on 2023-08-25. Score: 1

Thank you very much for your response which is very clear :
Yes, the recipe.allergens is an array which contains alergene types with an id and a name properties.
-> In DB : Allergen(id, name).
I tried your solution but it doesn't work. I don't think it is the good way to do...
{% set allergensArray = [] %}
    {% for allergen in recipe.allergenes %}
      {% set singleElement = {'id' : allergen.id, 'name' : allergen.name} %}
      {% set allergensArray = allergensArray|merge({singleElement}) %}
    {% endfor %}
    {{ dump(allergensArray) }}

The result is :

I am googling for a solution but not found for the moment.
Thank you again

> **Comments:**
> **username_2**: Inside your merge, you need to wrap the singleElement with square bracket instead curly bracket, so it would allergensArray|merge([singleElement])  

---

