<p>You can achieve this with Craft's <a href="https://docs.craftcms.com/v2/templating/functions.html#shuffle-array" rel="nofollow noreferrer"><code>shuffle()</code> method</a> and Twig's <a href="https://twig.symfony.com/doc/2.x/filters/slice.html" rel="nofollow noreferrer">slice filter</a> (like you mentioned in your answer).</p>

<pre><code>{% set newsletterImages = newsletter.imagesCongratulations.all() %}
{% set newsletterImages = newsletterImages | merge(newsletter.imagesCis.all()) %}
{% set newsletterImages = newsletterImages | merge(newsletter.imagesSection3.all()) %}

{% set shuffledImages = shuffle(newsletterImages) %}  // Shuffle our "master" array

{% for image in shuffledImages[:6] %}  // Get the first 6 images
  {{ image.title }}&lt;br&gt;
{% endfor %}
</code></pre>

CC BY-SA 4.0
2019-11-06T16:11:01.080Z
Thanks Jalen. Just saw this after I updated my answer. 
But I prefer your naming approach :-)
Terry Upton
CC BY-SA 4.0
2019-11-06T15:43:07.810Z
2019-11-06T15:43:07.810Z
Jalen Davenport
<p>Here is the complete code that achieves what I want. 
In case it is useful to anyone else.</p>

<pre><code>{% set newsletterImages = newsletter.imagesCongratulations.all() %}
{% set newsletterImages = newsletterImages | merge(newsletter.imagesCis.all()) %}
{% set newsletterImages = newsletterImages | merge(newsletter.imagesSection3.all()) %}

{% set shuffledImages = shuffle(newsletterImages) %}

{% set transformedImages = [] %}

{% for image in shuffledImages[:6] %}
  {% set transformedImage = craft.imager.transformImage(image, imageTransformParams) %}
  {% set transformedImages = transformedImages|merge([transformedImage]) %}
{% endfor %}
</code></pre>

CC BY-SA 4.0
2019-11-06T15:32:45.820Z
2019-11-06T16:09:30.790Z
2019-11-06T16:09:30.790Z
Terry Upton
<pre><code>{% set newsletterImages = newsletter.imagesCongratulations|merge(newsletter.imagesCis)|merge(newsletter.imagesSection3) %}
{% set transformedImages = newsletterImages %}

{% for image in newsletterImages %}
  {{ image.title }}&lt;br /&gt;

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

<p>newsletterImages is merging images from 3 different global fields.
I want to then randomise and only output 6 of these images.</p>

<p>However, when I try </p>

<pre><code>{% set transformedImages = newsletterImages.orderBy('RAND()').limit(6) %}
</code></pre>

<p>I get the error: Impossible to invoke a method ("orderBy") on an array. and Impossible to invoke a method ("limit") on an array.</p>

<p>Can anyone help point me in the right direction to achieve this?</p>

<templating><array>
Impossible to invoke a method on array. How to merge multiple objects and keep as objects
# Impossible to invoke a method on array. How to merge multiple objects and keep as objects

(Asked by: username_0 on 2019-11-06)

{% set newsletterImages = newsletter.imagesCongratulations|merge(newsletter.imagesCis)|merge(newsletter.imagesSection3) %}
{% set transformedImages = newsletterImages %}

{% for image in newsletterImages %}
  {{ image.title }}

{% endfor %}

newsletterImages is merging images from 3 different global fields.
I want to then randomise and only output 6 of these images.
However, when I try 
{% set transformedImages = newsletterImages.orderBy('RAND()').limit(6) %}

I get the error: Impossible to invoke a method ("orderBy") on an array. and Impossible to invoke a method ("limit") on an array.
Can anyone help point me in the right direction to achieve this?

**Tags:** templating, array


---

## 2 answers

### Answer by username_1 on 2019-11-06. Score: 2 | Accepted answer

You can achieve this with Craft's shuffle() method and Twig's slice filter (like you mentioned in your answer).
{% set newsletterImages = newsletter.imagesCongratulations.all() %}
{% set newsletterImages = newsletterImages | merge(newsletter.imagesCis.all()) %}
{% set newsletterImages = newsletterImages | merge(newsletter.imagesSection3.all()) %}

{% set shuffledImages = shuffle(newsletterImages) %}  // Shuffle our "master" array

{% for image in shuffledImages[:6] %}  // Get the first 6 images
  {{ image.title }}
{% endfor %}

> **Comments:**
> **username_0**: Thanks Jalen. Just saw this after I updated my answer.  But I prefer your naming approach :-)  

---

### Answer by username_0 on 2019-11-06. Score: 1

Here is the complete code that achieves what I want. 
In case it is useful to anyone else.
{% set newsletterImages = newsletter.imagesCongratulations.all() %}
{% set newsletterImages = newsletterImages | merge(newsletter.imagesCis.all()) %}
{% set newsletterImages = newsletterImages | merge(newsletter.imagesSection3.all()) %}

{% set shuffledImages = shuffle(newsletterImages) %}

{% set transformedImages = [] %}

{% for image in shuffledImages[:6] %}
  {% set transformedImage = craft.imager.transformImage(image, imageTransformParams) %}
  {% set transformedImages = transformedImages|merge([transformedImage]) %}
{% endfor %}


---

