Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate over array content without knowing the keys values

Tags:

twig

symfony

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>
like image 430
Joey Pablo Avatar asked Feb 02 '26 13:02

Joey Pablo


2 Answers

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>
like image 93
Matteo Avatar answered Feb 05 '26 07:02

Matteo


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.

like image 30
Artamiel Avatar answered Feb 05 '26 09:02

Artamiel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!