# Random entry from structure (workaround)

**URL:** <https://forum.getkirby.com/t/random-entry-from-structure-workaround/4088>\
**Category:** Questions\
**Tags:** v2\
**Created:** [May 10, 2016, 5:57pm UTC](https://forum.getkirby.com/t/random-entry-from-structure-workaround/4088 "2016-05-10T17:57:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![AhoyLemon](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/ahoylemon/32/4762_2.png) [@AhoyLemon](https://forum.getkirby.com/u/AhoyLemon)\
**Post date:** [May 10, 2016, 5:57pm UTC](https://forum.getkirby.com/t/random-entry-from-structure-workaround/4088/1 "2016-05-10T17:57:01Z")

</div>

Hey there. I noticed @texnixe opened [this issue](https://github.com/getkirby/kirby/issues/288) up on GitHub back in March, but while it’s in the hopper, I’m wondering if someone could help me solve this another way. Basically, here’s the idea:  
I have a structure field set up that asks for a couple pieces of information (name, image, etc), and what I want to do is have it so, on page load, the site picks a random entry in the structure and displays that information.

I’ve tried various versions of `$page->structure_field()->shuffle()->toStructure()->first();`, (with `->shuffle()` moved to different spots) but either it doesn’t work (breaks the page) or simply displays the first one in the structure (ignoring the shuffle).

Could I get a pointer of an alternate way to solve this? I can count the number of fields in that structure and generate a random number based on that, but I’m not sure how to select that specific thing out of the structure.

Something like…

```auto
$luckies = $page->lucky_people()->toStructure()->count();
$r = rand(1, $luckies);
$randomLucky = $page->lucky_people()->toStructure()->children($r);

```

…except not that, because that’s failing to work.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [May 10, 2016, 6:07pm UTC](https://forum.getkirby.com/t/random-entry-from-structure-workaround/4088/2 "2016-05-10T18:07:29Z")

</div>

You could try the `nth()` method. As an alternative, use the `toYaml()` method instead of `toStructure()` and then shuffle the resulting array. I had the same problem a while ago, can’t remember how I solved it in the end.

---

<div class="post-metadata">

**Author:** ![AhoyLemon](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/ahoylemon/32/4762_2.png) [@AhoyLemon](https://forum.getkirby.com/u/AhoyLemon)\
**Post date:** [May 10, 2016, 6:49pm UTC](https://forum.getkirby.com/t/random-entry-from-structure-workaround/4088/3 "2016-05-10T18:49:46Z")

</div>

There it is! `nth()` was what I was looking for. Final version, for those that find this page…

```
<?php 
    $luckies = $page->lucky_people()->toStructure()->count() - 1;
    $r = rand(0, $luckies);
    $randomLucky = $page->lucky_people()->toStructure()->nth($r);
?>

```

now you can call on `$randomLucky` however you like, ex:  
`<header style="background-image:url(<?php echo $randomLucky->background_mobile()->toFile()->url(); ?>)">`

Edited to compensate for 0 index.

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [July 12, 2016, 3:03pm UTC](https://forum.getkirby.com/t/random-entry-from-structure-workaround/4088/4 "2016-07-12T15:03:08Z")

</div>

Shuffling structure collections will finally work in Kirby 2.3.1. 🙂

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 4:57pm UTC](https://forum.getkirby.com/t/random-entry-from-structure-workaround/4088/5 "2025-02-06T16:57:33Z")

</div>


