php Symfony2 / Twig - 从动态数组键获取数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15606171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Symfony2 / Twig - getting array from dynamic array key
提问by user2143356
In PHP I would do this:
在 PHP 中,我会这样做:
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.
我看不到如何在 Twig(在 Symfony2 中)中做到这一点。我尝试了各种方法,但这似乎是显而易见的答案,但它不起作用。它返回一个“项目“the_index”,因为“数组”不存在“错误”。
{% for value in array %}
{% set the_index = loop.index %}
{{ another_array.the_index }}
Any ideas?
有任何想法吗?
回答by DonCallisto
The fastest way:
最快的方法:
{% for key,value in array %}
{{ another_array[key] }}
{% endfor %}
回答by Thomas Potaire
回答by Atanas Beychev
<ul>
{% for value in array %}
{% set the_index = attribute(another_array, loop.index) %}
<li>{{ the_index }}</li>
{% endfor %}
</ul>