クイズ作成サイトを運営しています。質問に対する回答をシャッフルされた順序でユーザーに表示したいと思います。
ランダムにシャッフルする場合は、回答がユーザーに提示された順序を保存しないようにしています。
後で(結果を表示するときに)同じようにシャッフルを繰り返すことができるように、予想どおりに回答をシャッフルしたいと思います。
回答のリストを特定の番号でシャッフルできると思いました(並べ替え内の番号を使用するか、ID番号で識別できる複数の種類の並べ替えを使用します。 これにより、シャッフルされた番号を簡単に保存できます。その番号を思い出して、同じ順序に再シャッフルします。
これが私がこれまでに持っているものの骨組みですが、答えをシャッフルされた順序で$shuffled_arrayに戻すロジックがありません。
<?php
function SortQuestions($answers, $sort_id)
{
// Blank array for newly shuffled answer order
$shuffled_answers = array();
// Get the number of answers to sort
$answer_count = count($questions);
// Loop through each answer and put them into the array by the $sort_id
foreach ($answers AS $answer_id => $answer)
{
// Logic here for sorting answers, by the $sort_id
// Putting the result in to $shuffled_answers
}
// Return the shuffled answers
return $shuffled_answers;
}
// Define an array of answers and their ID numbers as the key
$answers = array("1" => "A1", "2" => "A2", "3" => "A3", "4" => "A4", "5" => "A5");
// Do the sort by the number 5
SortQuestions($answers, 5);
?>
関数に渡された数値で回答をシャッフルするために使用できる手法はありますか?