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;