Joey Pablo
Joey Pablo

Reputation: 327

Iterate over array content without knowing the keys values

Can someone tell me how to iterate over array keys without knowing their values ? For example:

array:3 [▼
  "name" => "AccountId"
  "activated" => "false"
  "type" => "string"
]

I'd like to get a select list with:

<select name="select">
  <option>name: AccountId</option>
  <option>activated: false</option>
  <option>type: string</option>
</select>

Upvotes: 3

Views: 194

Answers (2)

Matteo
Matteo

Reputation: 39370

You can Iterating over Keys and Values as described in the doc, as example:

<select name="select">
    {% for key, value in users %}
       < option > {{ key }}: {{ value }}</option >
    {% endfor %}
</select>

Upvotes: 5

Artamiel
Artamiel

Reputation: 3762

Something like this should do the trick:

<select>
    {% for key, value in data %}
        <option>{{ key }}: {{ value }}</option>
    {% endfor %}
</select>

You can read more about for loop in Twig's documentation.

Upvotes: 3

Related Questions