PHP实现一个在线考试系统,如果实现自动组卷,试卷内题目不重复,选择题ABCD顺序随机打乱,题目顺序打乱?
// 此函数来自 http://php.p2hp.com/manual/zh/function.shuffle.php
// 作者 Antonio Ognio
function shuffle_with_keys(&$array) {
/* Auxiliary array to hold the new order */
$aux = array();
/* We work with an array of the keys */
$keys = array_keys($array);
/* We shuffle the keys */
shuffle($keys);
/* We iterate thru' the new order of the keys */
foreach($keys as $key) {
/* We insert the key, value pair in its new order */
$aux[$key] = $array[$key];
/* We remove the element from the old array to save memory */
unset($array[$key]);
}
/* The auxiliary array with the new order overwrites the old variable */
$array = $aux;
}
$optionText = array('A', 'B', 'C', 'D'); // 四个选项的名字
// 注意问题的索引 1 2 3 4 ... 要和答案的索引对应
$questions = array(
1 => 'php的官方网址是?'
,2 => 'php是哪一年发明的?'
,3 => 'php之父是谁?'
,4 => 'php目前最新的版本是?'
);
$answers = array(
1 => array(
'php.org'
,'php.com'
,'php.net',
'php.cn'
)
,2 => array(
'1990'
,'2000'
,'1999'
,'1994'
)
,3 => array(
'Rasmus Lerdorf'
,'Bill Gates'
,'Steve Jobs'
,'Larry Page'
)
,4 => array(
'5.0'
,'6.0'
,'7.0'
,'8.0'
)
);
shuffle_with_keys($questions);
$i = 1;
foreach($questions as $k => $q) {
shuffle($answers[$k]);
echo "<p>$i. $q</p>";
foreach($answers[$k] as $key => $answer) {
echo "<p>($optionText[$key]) $answer</p>";
}
$i++;
}