I'm building an advent calendar in PHP 7 and want to show dates 1 - 24 in a random order.
I've got a jquery plugin which can randomise my div elements, but it's not very good, and I want to know how to do it in PHP.
My code to output the dates looks (in simplified terms) like this:
for ($d = 1; $d <= 24; $d++) {
echo $d;
}
My plan was to instead use rand(1, 24) then store any numbers that had been generated in an array, e.g.
$date = rand(1, 24);
$used_dates[] = $date;
Then check $used_dates when picking a new date, e.g.
$unique_date = false;
while (!$unique_date) {
$date = rand(1, 24);
if (!in_array($date, $used_dates)) {
$used_dates[] = $date;
$unique_date = true;
}
}
This seems inefficient though. Are there better ways?