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

时间:2013-03-25 01:09:48

标签: php symfony twig

在PHP中我会这样做:

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

    echo $another_array[$key];

}

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

{% for value in array %}

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

有什么想法吗?

3 个答案:

答案 0 :(得分:33)

最快的方式:

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

答案 1 :(得分:23)

您可以使用attribute function

{{ attribute(another_array, the_index) }}

答案 2 :(得分:0)

computation <- function() # simplified version!
{
    # a lot of big matrices here....
    big_matrix <- matrix(rnorm(2000*2000), nrow = 2000, ncol = 2000)

    exp.value <- 4.5
    prior <- function (x) rep(exp.value, nrow(x))
    environment(prior) <- list2env(list(exp.value = exp.value)) # this is the update

    # after computation, it returns the model
    list(
        some_info = 5.18,
        prior = prior
    )
}
m <- computation()
save(m, file = "tmp.Rdata")
file.info("tmp.Rdata")$size
# [1] 30713151