1

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 :)

3
  • How do you know that you'll choose 'a' or 'b' ? Commented Apr 6, 2015 at 14:38
  • Can you add expected output, please? Commented Apr 6, 2015 at 14:47
  • Fixed with {% for j in array[id] %} Using '[]' instead of '.' works fine! Thanks :) Commented Apr 6, 2015 at 14:47

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.