Php 将细枝变量用作其他细枝变量的属性名称

Php 将细枝变量用作其他细枝变量的属性名称,php,symfony,twig,Php,Symfony,Twig,我必须使用一个细枝变量作为另一个细枝变量的属性 在for循环中,我获取特定实体的属性,并希望使用这些属性获取另一个for循环中实体变量的属性内容 一些代码可以清楚地说明这一点: {% for entity in entities %} {{entity.foo}}, {{entity.bar}}<br /> {% for property in specialdynamicproperties %} {{entity.property}} <!-- property ha

我必须使用一个细枝变量作为另一个细枝变量的属性

在for循环中,我获取特定实体的属性,并希望使用这些属性获取另一个for循环中实体变量的属性内容

一些代码可以清楚地说明这一点:

{% for entity in entities  %}

{{entity.foo}}, {{entity.bar}}<br />

{% for property in specialdynamicproperties %}
{{entity.property}} <!-- property has the content foobar for example, I want to use it as the property accessor for entity -->
{% endfor %}

{% endfor %}
{%用于实体中的实体%}
{{entity.foo},{{entity.bar}}
{specialdynamicproperties%中属性的%s} {{entity.property} {%endfor%} {%endfor%}
谢谢。

这就是您要找的

    {% for object in objects %}
        {% for column in columns %}
            {{ attribute(object, column) }} {# equivalent to php $object[$column] #}
        {% endfor %}
    {% endfor %}

使用Twig(Twig>1.2)

你试过这个吗?@mbosecke:这个对我有用,写下来作为答案,我就可以接受了,谢谢:)