我对变量变量的概念不熟悉,我认为我并不完全理解它。
我要做的是创建一个 for 循环,该循环将根据可变数量的变量填充数组。
我正在尝试替换:(手动硬编码)
$numCorrectArray = array(1=>$q01TotalCorrect, 2=>$q02TotalCorrect, 3=>$q03TotalCorrect, 4=>$q04TotalCorrect, 5=>$q05TotalCorrect, 6=>$q06TotalCorrect, 7=>$q07TotalCorrect, 8=>$q08TotalCorrect, 9=>$q09TotalCorrect, 10=>$q10TotalCorrect, 11=>$q11TotalCorrect, 12=>$q12TotalCorrect, 13=>$q13TotalCorrect, 14=>$q14TotalCorrect, 15=>$q15TotalCorrect, 16=>$q16TotalCorrect, 17=>$q17TotalCorrect, 18=>$q18TotalCorrect, 19=>$q19TotalCorrect, 20=>$q20TotalCorrect, 21=>$q21TotalCorrect, 22=>$q22TotalCorrect, 23=>$q23TotalCorrect, 24=>$q24TotalCorrect, 25=>$q25TotalCorrect, 26=>$q26TotalCorrect, 27=>$q27TotalCorrect, 28=>$q28TotalCorrect, 29=>$q29TotalCorrect);
与:(动态)
$numCorrectArray = array();
for($i=1; $i <= $stats->numberOfQuestions; $i++) {
if($i < 10) {
$questionNumber = "0" . $i;
} else {
$questionNumber = $i;
}
$varName = "q" . $questionNumber . "TotalCorrect";
array_push($numCorrectArray, $$varName);
}
我将如何做到这一点?谢谢