<p>Just put the field names in parentheses:</p>

<pre><code>{% set options = [] %}
{% for event in events %}
  {% set options = options|merge({ (event.slug):(event.eventTitle) }) %}
{% endfor %}
</code></pre>

<p>And now I can output them in the format Craft wants for a form field:</p>

<pre><code>{{ forms.selectField({
  label: "Choose event"|t,
  name: "eventSlug",
  options: options,
}) }}
</code></pre>

CC BY-SA 4.0
2018-07-17T23:40:09.470Z
2018-07-17T23:40:09.470Z
4midori
<p>I grab some values via an element query:</p>

<pre><code>{% set events = craft.entries.section('events').status('live,pending').order('postDate asc') %}
</code></pre>

<p>But I need to put them into an array for use in a Craft-styled form field.</p>

<p>Twig documentation shows how to put text values in an array and merge them:</p>

<pre><code>{% set items = items|merge({ 'peugeot': 'car', 'renault': 'car' }) %}
</code></pre>

<p>But how do I do this with the results of my element query?</p>

<templating><merge><elementquery>
What is the correct syntax for using merge to create an array of field values from an element query?
# What is the correct syntax for using merge to create an array of field values from an element query?

(Asked by: username_0 on 2018-07-17)

I grab some values via an element query:
{% set events = craft.entries.section('events').status('live,pending').order('postDate asc') %}

But I need to put them into an array for use in a Craft-styled form field.
Twig documentation shows how to put text values in an array and merge them:
{% set items = items|merge({ 'peugeot': 'car', 'renault': 'car' }) %}

But how do I do this with the results of my element query?

**Tags:** templating, merge, elementquery

---

## 1 answers

### Answer by username_0 on 2018-07-17. Score: 1

Just put the field names in parentheses:
{% set options = [] %}
{% for event in events %}
  {% set options = options|merge({ (event.slug):(event.eventTitle) }) %}
{% endfor %}

And now I can output them in the format Craft wants for a form field:
{{ forms.selectField({
  label: "Choose event"|t,
  name: "eventSlug",
  options: options,
}) }}


---

