Symfony2/Twig - 从动态数组键获取数组

use*_*356 17 php symfony twig

在PHP中我会这样做:

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

    echo $another_array[$key];

}
Run Code Online (Sandbox Code Playgroud)

我无法在Twig中看到如何做到这一点(在Symfony2中).我尝试了各种各样的东西,但这似乎是明显的答案,但它不起作用.它返回一个'Item"the_index"for"Array"在'错误中不存在'.

{% for value in array %}

    {% set the_index = loop.index %}
    {{ another_array.the_index }}
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

Don*_*sto 37

最快的方法:

{% for key,value in array %}
  {{ another_array[key] }}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)


Tho*_*ire 24

您可以使用属性功能.

{{ attribute(another_array, the_index) }}
Run Code Online (Sandbox Code Playgroud)