<p>Take a look at the <code>shuffle</code> function provided by Craft: <a href="https://craftcms.com/docs/templating/functions#shuffle" rel="nofollow">https://craftcms.com/docs/templating/functions#shuffle</a></p>

<p>This will shuffle the complete array and output them all in the new order:</p>

<pre><code>{% set img = [
    { bildli: 'http://i.giphy.com/I0xDz0htjsiME.gif' },
    { bildli: 'https://i.giphy.com/fA3JALhIi5YVa.gif' }
] %}

{% set shuffledImages = shuffle(img) %}

{% for item in shuffledImages %}
    &lt;img src="{{ item.bildli }}" alt=""&gt;
{% endfor %}
</code></pre>

<p>If you want to just output one random image, then this should work:</p>

<pre><code>{% set img = [
    { bildli: 'http://i.giphy.com/I0xDz0htjsiME.gif' },
    { bildli: 'https://i.giphy.com/fA3JALhIi5YVa.gif' }
] %}

{% set randomImage = shuffle(img) | first %}

&lt;img src="{{ randomImage.bildli }}" alt=""&gt;
</code></pre>

CC BY-SA 3.0
2015-12-22T15:20:31.603Z
2015-12-22T15:20:31.603Z
Steve Rowling
<p>How do I output one random line of an array?</p>

<pre><code>{% set img = [
  { bildli: 'http://i.giphy.com/I0xDz0htjsiME.gif' },
  { bildli: 'https://i.giphy.com/fA3JALhIi5YVa.gif' }
 ] %}
 {% for item in img %}
   &lt;img src="{{ item.bildli }}" alt=""&gt;
 {% endfor %}
</code></pre>

<p>I tried <code>{% for item in img.limit(1).order('RAND()') %}</code> like you would on a normal <code>entries.section</code> but that doesn't work on array.</p>

<p>I also tried: <code>{% for item in random(img) %}</code> as statet in the twig doc but that doesn't work eyther. What's the catch here?</p>

<array><random>
Apply 'random' to an array
# Apply 'random' to an array

(Asked by: username_0 on 2015-12-22)

How do I output one random line of an array?
{% set img = [
  { bildli: 'http://i.giphy.com/I0xDz0htjsiME.gif' },
  { bildli: 'https://i.giphy.com/fA3JALhIi5YVa.gif' }
 ] %}
 {% for item in img %}
   
 {% endfor %}

I tried {% for item in img.limit(1).order('RAND()') %} like you would on a normal entries.section but that doesn't work on array.
I also tried: {% for item in random(img) %} as statet in the twig doc but that doesn't work eyther. What's the catch here?

**Tags:** array, random

---

## 1 answers

### Answer by username_1 on 2015-12-22. Score: 4 | Accepted answer

Take a look at the shuffle function provided by Craft: https://craftcms.com/docs/templating/functions#shuffle
This will shuffle the complete array and output them all in the new order:
{% set img = [
    { bildli: 'http://i.giphy.com/I0xDz0htjsiME.gif' },
    { bildli: 'https://i.giphy.com/fA3JALhIi5YVa.gif' }
] %}

{% set shuffledImages = shuffle(img) %}

{% for item in shuffledImages %}
    
{% endfor %}

If you want to just output one random image, then this should work:
{% set img = [
    { bildli: 'http://i.giphy.com/I0xDz0htjsiME.gif' },
    { bildli: 'https://i.giphy.com/fA3JALhIi5YVa.gif' }
] %}

{% set randomImage = shuffle(img) | first %}


---

