Skip to content

Instantly share code, notes, and snippets.

@jlabs
Created February 1, 2021 09:19
Show Gist options
  • Save jlabs/dd7a43b03a13b7cece4093870c5a0c5c to your computer and use it in GitHub Desktop.
Save jlabs/dd7a43b03a13b7cece4093870c5a0c5c to your computer and use it in GitHub Desktop.
Get random values from an array
{# Get the destinations from an entry query #}
{% set originalArrayQ = craft.entries().section("helloworld") %}
{# Turn the query into an array of results #}
{% set originalArray = originalArrayQ.all() %}
{# Create an empty array #}
{% set r = [] %}
{# Loop through an x amount of count e.g. 0 to 3 (4 values) #}
{% for item in 0..3 %}
{# Merge into the array above a random value from the original array that isn't already in the random (r) array #}
{% set r = r | merge([random(originalArray | filter(v => v not in r))]) %}
{% endfor %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment