1

I would like to select the second position in the array below. The problem is that this number is automatically changed. So I try to do this with .index function from Twig but I can not figure it out. Can someone help me?

array ( 7296680 => array ( ... ), 7296635 => array ( ... 'url' => 'vesten', 'title' => 'Vesten', ), )

2

You can use the slice filter but it will return an array, so you either have to loop the result or access the first index

{% for item in array|slice(1, 1) %}
    {{ dump(item) }}
{% endfor %}
1

I think you can do {{ myarray[1] }} or even {{ myarray.1 }}

A more convoluted way would be to loop through the items and on the second pass, get the value.

{% set secondValue = null %}
{% for value in myArray %}
    {% if loop.index == 1 %}
        {% set secondValue = value %}
    {% endif %}
{% endfor %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.