Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 / Twig - getting array from dynamic array key

Tags:

php

twig

symfony

In PHP I would do this:

foreach( $array as $key => $value ) {

    echo $another_array[$key];

}

I can't see how to do that in Twig (in Symfony2). I've tried various things, but this would seem the obvious answer, but it doesn't work. It returns a 'Item "the_index" for "Array" does not exist in' error.

{% for value in array %}

    {% set the_index = loop.index %}
    {{ another_array.the_index }}

Any ideas?

like image 807
user2143356 Avatar asked Mar 25 '13 01:03

user2143356


2 Answers

The fastest way:

{% for key,value in array %}
  {{ another_array[key] }}
{% endfor %}
like image 152
DonCallisto Avatar answered Sep 22 '22 16:09

DonCallisto


You can use the attribute function.

{{ attribute(another_array, the_index) }}
like image 25
Thomas Potaire Avatar answered Sep 19 '22 16:09

Thomas Potaire