About flatten an array

Hi,

In my controller I made a variable[] and inside it there is all the event pages which are before today.

This variable is called $events_events_past .

So I want to get all images that are inside a specific structure field in all these pages.

And then I want to shuffle them all.

If i do this :

<?php foreach($events_events_past as $event): ?>

    <?php $event_photographies = $event->photographies()->toStructure() ?>
        <?php foreach ($event_photographies as $event_photography): ?>
        <?php echo $event_photography ?><br>
    <?php endforeach ?>

<?php endforeach ?>

I get the list that Iā€™m looking for :

a-image1.jpg
a-image2.jpg
a-image3.jpg
a-image4.jpg
a-image5.jpg
b-image1.jpg
b-image2.jpg
b-image3.jpg
c-image1.jpg
c-image2.jpg
c-image3.jpg
c-image4.jpg
ā€¦

If I had the shuffle :

<?php foreach($events_events_past as $event): ?>

    <?php $event_photographies = $event->photographies()->toStructure() ?>
    <?php foreach ($event_photographies->shuffle() as $event_photography): ?>
        <?php echo $event_photography ?><br>
    <?php endforeach ?>

<?php endforeach ?>

It mix all images by page and not all together like I would like :

a-image5.jpg
a-image1.jpg
a-image3.jpg
a-image4.jpg
a-image2.jpg
b-image1.jpg
b-image3.jpg
b-image2.jpg
c-image4.jpg
c-image3.jpg
c-image2.jpg
c-image1.jpg
ā€¦

No surprise Iā€™m noob with php, for many hours I tried to do it with merge, array_merge etc.

Now Iā€™m asking for help,

Thanks already !

You did great so far @Polyactifs! To continue from where you left off, add each photo to an array then shuffle it:

<?php
$photographies = [];

foreach($events_events_past as $event) {
    foreach ($event->photographies()->toStructure() as $event_photography) {
        $photographies[] = $event_photography->value();
    }
}

shuffle($photographies);

?>

<?php foreach ($photographies as $photography): ?>
    <?php echo $photography ?><br>
<?php endforeach ?>
1 Like

Thanks so much !

It works perfectly, I just had to remove ->value() from the line :

$photographies[] = $event_photography->value();

Again, big thanks !

I can finish my project now :slight_smile: