<p>Lindsey's answer should be the accepted one, but just for kicks – here's an additional approach that makes sense if you need an array of IDs <em>and</em> you want to loop over the entries, as well. Calling <code>.ids()</code> on the ElementCriteriaModel will result in an additional database query, but using the <code>|group</code> and <code>|keys</code> filters won't:</p>

<pre><code>{% set authorEntries = craft.users.search('fullName:*' ~ query ~ '*').find() %}
{% set authorIds = authorEntries|group('id')|keys %}

{% for authorId in authorIds %}
    ...
{% endfor %}

{% for author in authorEntries %}
   ...
{% endfor %}
</code></pre>

CC BY-SA 3.0
2016-10-06T22:51:20.710Z
Oh that's cool. Thanks Mats. Craft is tied so nicely in with Twig!
JamesNZ
CC BY-SA 3.0
2016-10-06T22:14:03.303Z
2016-10-06T22:14:03.303Z
Mats Mikkel Rummelhoff
<p>Simply append <code>.ids()</code> to your initial query...</p>

<pre><code>{% set authorIds = craft.users.search('fullName:*' ~ query ~ '*').ids() %}
</code></pre>

<p>Without the <code>.ids()</code> method, you're actually working with an <a href="https://craftcms.com/docs/templating/elementcriteriamodel" rel="noreferrer"><strong>Element Criteria Model</strong></a>.</p>

<p>The behavior that you're seeing is the default... it's as if you had appended <code>.find()</code> to your initial query.</p>

<blockquote>
  <p>[<code>.find()</code>] will be called automatically as soon as the ElementCriteriaModel is treated like an array (that is, as soon as you...  start looping through the elements)</p>
</blockquote>

<p>So instead of allowing the default behavior to kick in, just apply <code>.ids()</code> to the initial query, and get a batch of entry IDs instead of a full array of entries</p>

CC BY-SA 3.0
2016-10-06T22:50:00.760Z
Thanks Lindsey. That's why I couldn't find it in the docs, I was looking in the wrong place! https://craftcms.com/docs/templating/elementcriteriamodel#fetching-the-elements
JamesNZ
CC BY-SA 3.0
2016-10-06T21:53:34.793Z
2016-10-06T21:53:34.793Z
Lindsey D
<p>If I've got an array of entries, is there a less long winded approach to convert that to an array of ids so I can use it in another lookup.</p>

<pre><code>{# Look up authors that match the entered query.
{% set authorEntries = craft.users.search('fullName:*' ~ query ~ '*')|default([]) %}

{% set authorIds = [] %}
{% for author in authorEntries %}
    {% set authorIds = authorIds|merge([author.id]) %}
{% endfor %}
</code></pre>

<p>Thanks!</p>

<templating><entries><search>
Converting array of entries to an array of ids
# Converting array of entries to an array of ids

(Asked by: username_0 on 2016-10-06)

If I've got an array of entries, is there a less long winded approach to convert that to an array of ids so I can use it in another lookup.
{# Look up authors that match the entered query.
{% set authorEntries = craft.users.search('fullName:*' ~ query ~ '*')|default([]) %}

{% set authorIds = [] %}
{% for author in authorEntries %}
    {% set authorIds = authorIds|merge([author.id]) %}
{% endfor %}

Thanks!

**Tags:** templating, entries, search

---

## 2 answers

### Answer by username_2 on 2016-10-06. Score: 5 | Accepted answer

Simply append .ids() to your initial query...
{% set authorIds = craft.users.search('fullName:*' ~ query ~ '*').ids() %}

Without the .ids() method, you're actually working with an Element Criteria Model.
The behavior that you're seeing is the default... it's as if you had appended .find() to your initial query.

[.find()] will be called automatically as soon as the ElementCriteriaModel is treated like an array (that is, as soon as you...  start looping through the elements)

So instead of allowing the default behavior to kick in, just apply .ids() to the initial query, and get a batch of entry IDs instead of a full array of entries

> **Comments:**
> **username_0**: Thanks Lindsey. That's why I couldn't find it in the docs, I was looking in the wrong place! https://craftcms.com/docs/templating/elementcriteriamodel#fetching-the-elements  

---

### Answer by username_1 on 2016-10-06. Score: 1

Lindsey's answer should be the accepted one, but just for kicks – here's an additional approach that makes sense if you need an array of IDs and you want to loop over the entries, as well. Calling .ids() on the ElementCriteriaModel will result in an additional database query, but using the |group and |keys filters won't:
{% set authorEntries = craft.users.search('fullName:*' ~ query ~ '*').find() %}
{% set authorIds = authorEntries|group('id')|keys %}

{% for authorId in authorIds %}
    ...
{% endfor %}

{% for author in authorEntries %}
   ...
{% endfor %}

> **Comments:**
> **username_0**: Oh that's cool. Thanks Mats. Craft is tied so nicely in with Twig!  

---

