<p>The <code>merge</code> filter only does a <em>shallow</em> merge. This means that if both the first and the second hash (in Twig, an <a href="https://twig.symfony.com/doc/3.x/templates.html#literals" rel="nofollow noreferrer">associative array is called a hash</a>) have the same key, the final result will contain just the value of the second array. What you're looking for is a <em>deep</em> merge, which will recursive merge nested arrays and hashes.</p>
<p>Luckily, Craft extends Twig's <code>merge</code> filter to <a href="https://craftcms.com/docs/4.x/dev/filters.html#merge" rel="nofollow noreferrer">support a deep merge</a>. Pass <code>true</code> as a second argument and the filter will perform a deep merge.</p>
<pre class="lang-twig prettyprint-override"><code>{% set newPlayer = { playerName: &quot;Henry Doe&quot;, PID: 8885551333, TID: 1} %}
{% set testArray = testArray|merge({ players: [newPlayer], }, true) %}
</code></pre>
<p>As a sidenote, Twig isn't really designed or intended for this type of complex data manipulation. In most cases, you should do this in PHP – you can find examples of how to extend Twig with PHP functions here: <a href="https://craftcms.com/docs/4.x/extend/extending-twig.html" rel="nofollow noreferrer">https://craftcms.com/docs/4.x/extend/extending-twig.html</a></p>

CC BY-SA 4.0
2023-06-23T08:41:31.220Z
2023-06-23T08:41:31.220Z
MoritzLost
<p>So I've read all the posts and followed along at home. I'm not a CS major. I have an array:</p>
<pre><code>{% set testArray = {
  name: &quot;John Doe&quot;,
  email: &quot;test@test.com&quot;,
  players: [
  {
    playerName: &quot;Jane Doe&quot;,
    PID: 8885551212,
    TID: 1234,
  },
  {
    playerName: &quot;Billy Doe&quot;,
    PID: 8285551,
    TID: 1222,
  },
],
follows : [
    '234',
    '345',
    '999',
]}%}
</code></pre>
<p>Great, it verks!! When I ask it to spit it out via: {{testArray|json_encode}}, it spits out:</p>
<pre><code>{&quot;name&quot;:&quot;John Doe&quot;,&quot;email&quot;:&quot;test@test.com&quot;,&quot;players&quot;:[{&quot;playerName&quot;:&quot;Jane Doe&quot;,&quot;PID&quot;:8885551212,&quot;TID&quot;:1234},{&quot;playerName&quot;:&quot;Billy Doe&quot;,&quot;PID&quot;:8285551,&quot;TID&quot;:1222}],&quot;follows&quot;:[&quot;234&quot;,&quot;345&quot;,&quot;999&quot;]}
</code></pre>
<p>Huzzah, go me!</p>
<p>Now, if I want to update the <code>players</code> array, I'm trying this</p>
<pre><code>{% set testA = { playerName:&quot;Henry Doe&quot;,PID: 8885551333,TID: 1} %}
{% set testArray = testArray|merge({ &quot;players&quot;: [testA]} ) %}
</code></pre>
<p>It'a good boi and does this BUT it overwrites the other 2 objects in the array and outputs</p>
<pre><code>{&quot;name&quot;:&quot;John Doe&quot;,&quot;email&quot;:&quot;test@test.com&quot;,&quot;players&quot;:[{&quot;playerName&quot;:&quot;Henry Doe&quot;,&quot;PID&quot;:8885551333,&quot;TID&quot;:1}],&quot;follows&quot;:[&quot;234&quot;,&quot;345&quot;,&quot;999&quot;]}
</code></pre>
<p>So, the question is A) is it even possible? b) WTAF am I doing wrong?</p>

<array><merge><object>
Update array inside array
# Update array inside array

(Asked by: username_0 on 2023-06-23)

So I've read all the posts and followed along at home. I'm not a CS major. I have an array:
{% set testArray = {
  name: "John Doe",
  email: "test@test.com",
  players: [
  {
    playerName: "Jane Doe",
    PID: 8885551212,
    TID: 1234,
  },
  {
    playerName: "Billy Doe",
    PID: 8285551,
    TID: 1222,
  },
],
follows : [
    '234',
    '345',
    '999',
]}%}

Great, it verks!! When I ask it to spit it out via: {{testArray|json_encode}}, it spits out:
{"name":"John Doe","email":"test@test.com","players":[{"playerName":"Jane Doe","PID":8885551212,"TID":1234},{"playerName":"Billy Doe","PID":8285551,"TID":1222}],"follows":["234","345","999"]}

Huzzah, go me!
Now, if I want to update the players array, I'm trying this
{% set testA = { playerName:"Henry Doe",PID: 8885551333,TID: 1} %}
{% set testArray = testArray|merge({ "players": [testA]} ) %}

It'a good boi and does this BUT it overwrites the other 2 objects in the array and outputs
{"name":"John Doe","email":"test@test.com","players":[{"playerName":"Henry Doe","PID":8885551333,"TID":1}],"follows":["234","345","999"]}

So, the question is A) is it even possible? b) WTAF am I doing wrong?

**Tags:** array, merge, object


---

## 1 answers

### Answer by username_1 on 2023-06-23. Score: 1 | Accepted answer

The merge filter only does a shallow merge. This means that if both the first and the second hash (in Twig, an associative array is called a hash) have the same key, the final result will contain just the value of the second array. What you're looking for is a deep merge, which will recursive merge nested arrays and hashes.
Luckily, Craft extends Twig's merge filter to support a deep merge. Pass true as a second argument and the filter will perform a deep merge.
{% set newPlayer = { playerName: "Henry Doe", PID: 8885551333, TID: 1} %}
{% set testArray = testArray|merge({ players: [newPlayer], }, true) %}

As a sidenote, Twig isn't really designed or intended for this type of complex data manipulation. In most cases, you should do this in PHP – you can find examples of how to extend Twig with PHP functions here: https://craftcms.com/docs/4.x/extend/extending-twig.html


---

