Collection shuffle and limit

Hi,

I’m trying to build a card deck that is holding 6 cards. The individual cards are subpages. I created a logic that is making sure every card is only once in the deck.

This logic is working. I echo’d $index and its always a unique number within the range.

Every few reloads I’m getting in the terminal → “zsh: segmentation fault php -S localhost:8081 kirby/router.php” and the Kirby has stopped.

Does anybody have an idea?

            $cnumber = $page->children()->listed()->count();
            $cardstock = [];
            for($x = 0; $x <= 5; $x++) {
                $new_card = rand(0, $cnumber - 1);
                $searching = true;
                while($searching) {
                    if(!in_array($new_card,$cardstock)) {
                        $cardstock[] = $new_card;
                        $searching = false;
                    }
                    else {
                        $new_card = rand(0, $cnumber - 1);
                    } 
                }
            }

            foreach ($cardstock as $index):
            $card = $page->children()->nth($index);
            if ($card->isNotEmpty()): ?>

You can use foreach ($page->children()->listed()->shuffle() as $card) and Kirby will handle the randomness for you.

@lukasbestle Thank You for your answer and possible solution.

I’ll have a closer look at shuffle. I believe I still need the random numbers, because I’m only selecting 5 cards out of 36. From what I understand, shuffle would generate a random index, but is not able to just return 5 random cards from the stack without duplicates.

Do you have any idea how to debug the segmentation fault?

You can limit the returned cards to 5 after shuffling using limit(5)

$page->children()->listed()->shuffle()->limit(5)

Or 6 for that matter…

This, btw, doesn’t make sense, because if $card is null, you’d get a “Calling isNotEmpty()` on null” error.

And the else statement is superfluous.

I switched to shuffle. It’s giving me partly what I need, because I need 2 card decks without duplicates. I’m thinking of using $page->children()->listed()->shuffle()->limit(10) to get 10 unique cards and split it in half.

I was under the impression the return value is an array and could be used as such for

$cardstock = $page->children()->listed()->shuffle()->limit(10);
            list($deck1, $deck2) = array_chunk($cardstock, 5);

but its telling me array_chunk(): Argument #1 ($array) must be of type array, Kirby\Cms\Pages given

@texnixe I removed the stuff that doesn’t made sense.

This is not an array, but a Pages object, so no, you cannot use array_chunk. There is a chunk method, but I don’t know if this would help a lot.

What you can do:

$cardstock = $page->children()->listed()->shuffle()->limit(10);
$deck1 = $cardstock->limit(5);
$deck2 = $cardstock->offset(5);
1 Like

Working as needed.

Is this something I should have figured out myself from the reference, the cookbook or the tutorials?

We have a cookbook recipe on collections in general. Detailed information can be found in the reference.