Symfony2/Twig - 从动态数组键获取数组 [英] Symfony2 / Twig - getting array from dynamic array key

查看:28
本文介绍了Symfony2/Twig - 从动态数组键获取数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 PHP 中我会这样做:

In PHP I would do this:

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

    echo $another_array[$key];

}

我看不到如何在 Twig(在 Symfony2 中)中做到这一点.我尝试了各种方法,但这似乎是显而易见的答案,但它不起作用.它返回一个项目the_index",因为数组"不存在"错误.

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 }}

有什么想法吗?

推荐答案

最快的方法:

{% for key,value in array %}
  {{ another_array[key] }}
{% endfor %}

这篇关于Symfony2/Twig - 从动态数组键获取数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆