以相同方式重新排序陣列我正在運行測驗製作網站。我希望以洗牌順序向用戶顯示問題的答案。按照編號
我試圖避免存儲回答提交給用戶,如果我隨機洗牌的順序。
我想隨機打亂答案,,這樣我可以在後面以相同的方式重複洗牌(顯示結果時)。
我以爲我可以將答案列表中的某個數字(使用排序內的數字或者可以通過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);
?>
是否有技術,我ca n用於通過傳遞給函數的數字來洗牌答案?
會保留在隱藏的輸入元素內的答案順序被視爲「存儲」? :) –