peace_love
peace_love

Reputation: 6461

How can I use a dynamic key/value on a second level in twig?

This is my group array:

array:4 [▼
  0 => Fields {#7444 ▼
    -id: 1
    -name: "ID"
    -unique_id: "6ab8c870ed"
    -productgroup: PersistentCollection {#7448 ▶}
    -type: Type {#7525 ▼
      +__isInitialized__: true
      -id: 2
      -name: "hidden"
      -unique_id: "5e1086c862"
      -label: "hidden"
       …2
    }
  }
  1 => Fields {#7526 ▶}
  2 => Fields {#7530 ▶}
  3 => Fields {#7534 ▶}
]

This is my column array:

array:3 [▼
  0 => "id"
  1 => "name"
  2 => "type"
]

I know that my id is 1:

$id = "1";

For each value in my column key I want to print out the according value from my group array.

So the result would be:

1
ID
hidden

I try to achieve this with twig

{% for key, value in column %}
   {% for k, v in group %}
      {{ v.[value] }};
   {% endfor %}
{% endfor %}

The error is:

Expected name or number.

NOTE: This Symfony2 / Twig - getting array from dynamic array key is not helping me, because it only explains how to use the value like this v[value] or this v[key] but not in the second level like this v.[value].

Upvotes: 0

Views: 1537

Answers (1)

DarkBee
DarkBee

Reputation: 15652

You need to use two for-loops in your code to achieve what you want though,

{% for class in classes %}
    if (optionValue == {{ class.id }}) {
        {% for column in columns %}
        var {{ column }} = '{{ attribute(class, column) }}';
        {% endfor %}        
    }
{% endfor %}

demo

Upvotes: 1

Related Questions