<p>Do you render the content of your array right after you inserted the elements? Yes? Then what about using a conditional checking the <code>loop.index</code> in your output loop to render your special inserted content at defined loop positions?</p>

<pre><code>{% if loop.index == 2 %}
{% endif %}
</code></pre>



<pre><code>{% if loop.index in [2, 5, 13] %}
{% endif %}
</code></pre>

CC BY-SA 3.0
2015-10-09T06:16:22.230Z
2015-10-09T06:16:22.230Z
carlcs
<p>You can accomplish this with <a href="http://twig.sensiolabs.org/doc/filters/slice.html" rel="nofollow"><code>slice</code></a> and <a href="http://twig.sensiolabs.org/doc/filters/merge.html" rel="nofollow"><code>merge</code></a>, although it's not very pretty:</p>

<pre><code>{% set myArray = ['a', 'b', 'd' ] %}
{% set myArray = myArray|slice(0,2)|merge(['c'])|merge(myArray|slice(2,myArray|length)) %}

{{ myArray|join(', ') }}
</code></pre>

<p>To explain what's happening above:</p>

<pre><code>{% set myArray = ['a', 'b', 'd' ] %}
</code></pre>

<p>This just sets an array we can work with. </p>

<pre><code>|slice(0,2)
</code></pre>

<p>The first slice filter "chops off" and returns the first two items in the original array (namely "a" and "b"). We do this because we want to add a value after the "b" (i.e. the second index).</p>

<pre><code>|merge(['c'])
</code></pre>

<p>The first <code>merge</code> filter appends the value we want to add – the letter "c" – to the first two items. As <code>merge</code> is only able to merge arrays, we need to wrap the c in another array.</p>

<pre><code>|merge(myArray|slice(2,myArray|length))
</code></pre>

<p>The inner part of this statement – the <code>slice</code> – pulls all the remaining values from the original array <em>after</em> the first two that we chopped off at the beginning, until the end of the array.</p>

<p>These values are then appended to the a, b and c with another <code>merge</code> statement.</p>

<pre><code>{{ myArray|join(', ') }}
</code></pre>

<p>This is just a test, and will print <code>a, b, c, d</code>.</p>

CC BY-SA 3.0
2015-10-08T15:41:19.483Z
This worked thanks. Took me a while to get it, and had to break it down into shorter arrays and then join them altogether, as I wanted to add 4 and it was getting complicated! Hopefully this won't have too many implications performance-wise.
Anna
CC BY-SA 3.0
2015-10-08T21:26:36.293Z
@Anna Happy you got it sorted! Twig quickly gets fairly unreadable when things gets a little complex – I added a more detailed explanation to my answer.
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2015-10-08T12:35:05.400Z
2015-10-08T21:24:40.200Z
2015-10-08T21:24:40.200Z
Mats Mikkel Rummelhoff
<p>Is it possible to insert items into specific positions in an array (rather than at the beginning or at the end), a bit like javascript splice? Say, if I wanted to add a value between items 2 and 3? Please note I want to add keys to the array rather than replace anything.</p>

<templating><array>
Adding items to an array
# Adding items to an array

(Asked by: username_0 on 2015-10-08)

Is it possible to insert items into specific positions in an array (rather than at the beginning or at the end), a bit like javascript splice? Say, if I wanted to add a value between items 2 and 3? Please note I want to add keys to the array rather than replace anything.

**Tags:** templating, array

---

## 2 answers

### Answer by username_2 on 2015-10-08. Score: 4 | Accepted answer

You can accomplish this with slice and merge, although it's not very pretty:
{% set myArray = ['a', 'b', 'd' ] %}
{% set myArray = myArray|slice(0,2)|merge(['c'])|merge(myArray|slice(2,myArray|length)) %}

{{ myArray|join(', ') }}

To explain what's happening above:
{% set myArray = ['a', 'b', 'd' ] %}

This just sets an array we can work with. 
|slice(0,2)

The first slice filter "chops off" and returns the first two items in the original array (namely "a" and "b"). We do this because we want to add a value after the "b" (i.e. the second index).
|merge(['c'])

The first merge filter appends the value we want to add – the letter "c" – to the first two items. As merge is only able to merge arrays, we need to wrap the c in another array.
|merge(myArray|slice(2,myArray|length))

The inner part of this statement – the slice – pulls all the remaining values from the original array after the first two that we chopped off at the beginning, until the end of the array.
These values are then appended to the a, b and c with another merge statement.
{{ myArray|join(', ') }}

This is just a test, and will print a, b, c, d.

> **Comments:**
> **username_0**: This worked thanks. Took me a while to get it, and had to break it down into shorter arrays and then join them altogether, as I wanted to add 4 and it was getting complicated! Hopefully this won't have too many implications performance-wise.  
> **username_2**: @Anna Happy you got it sorted! Twig quickly gets fairly unreadable when things gets a little complex – I added a more detailed explanation to my answer.  

---

### Answer by username_1 on 2015-10-09. Score: 1

Do you render the content of your array right after you inserted the elements? Yes? Then what about using a conditional checking the loop.index in your output loop to render your special inserted content at defined loop positions?
{% if loop.index == 2 %}
{% endif %}

{% if loop.index in [2, 5, 13] %}
{% endif %}


---

