Adding items to an array 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.
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.