Shuffle JSON array from structure

Hello
Last thing to wonder about my project is how to shuffle a JSON array from structure field ? I tried many things between array_rand and shuffle, I don’t find the solution.

For now, I have

{ artist: "...", name: "...", album: "...", … } // from project 1 structure 4
{ artist: "...", name: "...", album: "...", … } // from project 1 structure 2
{ artist: "...", name: "...", album: "...", … } // from project 1 structure 3
{ artist: "...", name: "...", album: "...", … } // from project 1 structure 1
{ artist: "...", name: "...", album: "...", … } // from project 2 structure 1
{ artist: "...", name: "...", album: "...", … } // from project 2 structure 4
{ artist: "...", name: "...", album: "...", … } // from project 2 structure 2
{ artist: "...", name: "...", album: "...", … } // from project 2 structure 3

I would like (for example)

    { artist: "...", name: "...", album: "...", … } // from project 1 structure 1
    { artist: "...", name: "...", album: "...", … } // from project 2 structure 1
    { artist: "...", name: "...", album: "...", … } // from project 1 structure 3
    { artist: "...", name: "...", album: "...", … } // from project 1 structure 4
    { artist: "...", name: "...", album: "...", … } // from project 2 structure 1
    { artist: "...", name: "...", album: "...", … } // from project 1 structure 2
    { artist: "...", name: "...", album: "...", … } // from project 2 structure 3
    { artist: "...", name: "...", album: "...", … } // from project 2 structure 4

My code is :

function createNewStructure($pages, $field) {
    $structure = new Structure();
    $key = 0;
    foreach($pages as $p) {
        foreach($p->$field()->toStructure()->shuffle() as $item) {
            $structure->append($key, $item)->shuffle();;
            $key++;
        }
    }
    return $structure;
}
$projects = page('projets')->children()->visible()->shuffle();

$items = createNewStructure($projects, 'addresses');


$json = array();
foreach($items as $item) {
    $json[] = array(
        'artist' => (string)$item->artist(),
        'name'  => (string)$item->name(),
        'album'  => (string)$item->album(),
        'url'   => (string)$item->fichier()->toFile()->url(),
        'date'   => (string)$item->date('d.m.y', 'time'),
        'cover_art_url' => url('assets/images/coverimage.png')
    );
}
$data = json_encode($json, JSON_UNESCAPED_SLASHES);
echo $data;

how about shuffling it before putting it in the array rather then shuffling the array.

You replied too fast I was editing my question :slight_smile: But as you can see, I tried to set shuffle() everywhere I could before, and it’s not really the result I’m looking for, like mentionned in comments

Just a wild guess…

...
foreach($items->shuffle() as $item) {
    $json[] = array(
        'artist' => (string)$item->artist(),
        'name'  => (string)$item->name(),
        'album'  => (string)$item->album(),
        'url'   => (string)$item->fichier()->toFile()->url(),
        'date'   => (string)$item->date('d.m.y', 'time'),
        'cover_art_url' => url('assets/images/coverimage.png')
    );
}
...

HAHA I definitely have to wait and think about twice before posting here, you make me feel stupid,
But thanks very much for fast response !!

Good night ! :slight_smile:

No worries, sometimes another pair of eyes is needed. i would take the other shuffles out and and just do it on that final foreach :zzz:

While I’d also shuffle the items, you can of course easily shuffle an array:

shuffle($array);

Or Kirby style:

a::shuffle($array);

Internally, Kirby does shuffle the array keys to shuffle the collection, anyway.

I think Amplitude.js also offers a shuffle mode…