<p>Getting that array is simple. Prepare an empty array, then loop through your entries and merge the "Sleeps" fields value to it.</p>

<p>To get your array with the unique values, you basically do the same but also save the value as the key on each iteration. If a key already exists in the array the key/value pair gets overwritten (→ good for you)!</p>

<p>There's a strange behaviour that setting custom numeric keys doesn't work with Twig, that's why I composed a string out of the value.</p>

<pre><code>{# Prepare variables #}
{% set myArray1 = [] %}
{% set myArray2 = [] %}

{# Loop through entries of the "Hotel Room" section #}
{% for entry in craft.entries.section('hotelRoom') %}

    {% set value = entry.sleeps %}

    {% set myArray1 = myArray1|merge([value]) %}
    {% set myArray2 = myArray2|merge({ ('key' ~ value): value }) %}

{% endfor %}
</code></pre>

CC BY-SA 3.0
2016-01-29T23:49:44.913Z
Using this method I've just hit an obvious problem. If the number of entries is high (~1500) Craft simply runs out of memory running this. Could we optimize this?
certainlyakey
CC BY-SA 3.0
2016-01-30T00:22:03.030Z
@certainlyakey I don't think so, not without switching to PHP in a custom plugin. You might want to open a new question for this...
carlcs
CC BY-SA 3.0
2014-12-12T20:38:44.150Z
Worked like a charm, thank you. Cunning idea using the key to avoid dupes!
Nick Dunn
CC BY-SA 3.0
2014-12-04T23:17:19.213Z
2014-12-04T23:37:36.547Z
2014-12-04T23:37:36.547Z
carlcs
<p>I have a section "Hotel Room" with a field "Sleeps" (number). There is no limit on the values that can be entered. On the frontend I want a list of all existing entered values for this field. So if I have these four entries:</p>

<ol>
<li>sleeps=2</li>
<li>sleeps=2</li>
<li>sleeps=8</li>
<li>sleeps=10</li>
</ol>

<p>I want an array [2, 2, 8, 10] to iterate over. Bonus points for unique/distinct values, but I'm well aware I can add a Twig filter to <code>array_unique()</code> it.</p>

<p>Is this possible using Craft's <code>craft.entries</code> lookups within Twig?</p>

CC BY-SA 3.0
2014-12-03T20:25:16.133Z
Do I understand this correctly if you want to pull in these vales and add them to an array that you can loop through in your template somewhere else?
naboovalley
<templating><entries><database><field>
Fetch values for a single field across all entries in a section
# Fetch values for a single field across all entries in a section

(Asked by: username_0 on 2014-12-03)

I have a section "Hotel Room" with a field "Sleeps" (number). There is no limit on the values that can be entered. On the frontend I want a list of all existing entered values for this field. So if I have these four entries:

1. sleeps=2
2. sleeps=2
3. sleeps=8
4. sleeps=10

I want an array [2, 2, 8, 10] to iterate over. Bonus points for unique/distinct values, but I'm well aware I can add a Twig filter to `array_unique()` it.

Is this possible using Craft's `craft.entries` lookups within Twig?

**Tags:** templating, entries, database, field

> **Comments:**
> **username_1**: Do I understand this correctly if you want to pull in these vales and add them to an array that you can loop through in your template somewhere else?  

---

## 1 answers

### Answer by username_2 on 2014-12-04. Score: 4 | Accepted answer

Getting that array is simple. Prepare an empty array, then loop through your entries and merge the "Sleeps" fields value to it.
To get your array with the unique values, you basically do the same but also save the value as the key on each iteration. If a key already exists in the array the key/value pair gets overwritten (→ good for you)!
There's a strange behaviour that setting custom numeric keys doesn't work with Twig, that's why I composed a string out of the value.
{# Prepare variables #}
{% set myArray1 = [] %}
{% set myArray2 = [] %}

{# Loop through entries of the "Hotel Room" section #}
{% for entry in craft.entries.section('hotelRoom') %}

    {% set value = entry.sleeps %}

    {% set myArray1 = myArray1|merge([value]) %}
    {% set myArray2 = myArray2|merge({ ('key' ~ value): value }) %}

{% endfor %}

> **Comments:**
> **username_0**: Worked like a charm, thank you. Cunning idea using the key to avoid dupes!  
> **username_3**: Using this method I've just hit an obvious problem. If the number of entries is high (~1500) Craft simply runs out of memory running this. Could we optimize this?  
> **username_2**: @username_3 I don't think so, not without switching to PHP in a custom plugin. You might want to open a new question for this...  

---

