0

以下 php 根据范围检查问题编号,并根据匹配条件分配一章文本。我不禁想到有更好、更快的方法来实现这一点。

$qnum = $questionArray[$key][questionNum];

if ($qnum <= 10){
    $chapterText = str_replace("full", "chapter1", $refTextBook);
}
elseif ($qnum > 10 && $qnum <= 20) {
    $chapterText = str_replace("full", "chapter2", $refTextBook);
}
elseif ($qnum > 20 && $qnum <= 30) {
    $chapterText = str_replace("full", "chapter3", $refTextBook);
}
elseif ($qnum > 30 && $qnum <= 40) {
    $chapterText = str_replace("full", "chapter4", $refTextBook);
}
elseif ($qnum > 40 && $qnum <= 50) {
    $chapterText = str_replace("full", "chapter5", $refTextBook);
}
elseif ($qnum > 50 && $qnum <= 60) {
    $chapterText = str_replace("full", "chapter6", $refTextBook);
}
elseif ($qnum > 60 && $qnum <= 70) {
    $chapterText = str_replace("full", "chapter7", $refTextBook);
}
elseif ($qnum > 70 && $qnum <= 80) {
    $chapterText = str_replace("full", "chapter8", $refTextBook);
}
elseif ($qnum > 80 && $qnum <= 90) {
    $chapterText = str_replace("full", "chapter9", $refTextBook);
}
elseif ($qnum > 90 && $qnum <= 100) {
    $chapterText = str_replace("full", "chapter10", $refTextBook);
}
elseif ($qnum > 100 && $qnum <= 110) {
    $chapterText = str_replace("full", "chapter11", $refTextBook);
}
elseif ($qnum > 110 && $qnum <= 120) {
    $chapterText = str_replace("full", "chapter12", $refTextBook);
}
elseif ($qnum > 120 && $qnum <= 130) {
    $chapterText = str_replace("full", "chapter13", $refTextBook);
}
elseif ($qnum > 130 && $qnum <= 140) {
    $chapterText = str_replace("full", "chapter14", $refTextBook);
}
elseif ($qnum > 140 && $qnum <= 150) {
    $chapterText = str_replace("full", "chapter15", $refTextBook);
}
4

1 回答 1

7
function getChapterName($qnum) {
    return "chapter".ceil($qnum / 10);
};
于 2012-08-18T04:38:02.867 回答