Created
February 1, 2021 09:19
-
-
Save jlabs/dd7a43b03a13b7cece4093870c5a0c5c to your computer and use it in GitHub Desktop.
Get random values from an array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# 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