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?