Twig用变量迭代

I have an array like this:

array['a'] = {x => 1, y => 2...}
array['b'] = {x => 5, y => 7...}

I need to iterate over the array, but in each case I need to enter only in the 'a' or 'b' which I choose.

{% for i in main %}
    {% set id = i.getId %}
    {% for j in array.id %}
         //do something like j.propertyA ...
    {% endfor %}
{% endfor %}

The fail is always get the error: "The key 'id' for array with keys 'a', 'b'... does not exist"

If I force writting:

{% for j in array.a %}

The program works fine always with array.a but I need to works with all automatically.

Any idea? Thanks :)

Change {% for j in array.id %} to {% for j in array[id] %}

This is because you're trying to access "id" (as is written) directly from array (and isn't defined). With [id] your variable is substitued with its value and so your call will not fail

I think you need array|keys twig filter. See more: http://twig.sensiolabs.org/doc/filters/keys.html.