使用一个关联数组中的值来测试另一个关联数组中的值

我需要帮助,尝试了好几天没有成功,PHP新手,所以请原谅我,我有一个从数据库表评分系统返回的关联数组,我想实现的是尝试来自另一个关联数组的“分数”,迭代评分系统,直到我找到一个分数,该分数落在评分系统中连续的值之间,然后返回字母等级和备注, 看到下面已经尝试过什么,我筋疲力尽,任何帮助将不胜感激。我尝试过的代码



while ($row = $grade->fetch(PDO::FETCH_ASSOC)) {

        $data = $row;

        var_export($data);

    } //fetches the grading system whose array is seen below


while($row = $stmt->fetch(PDO::FETCH_ASSOC)){

    $scores = $row;

    var_export($scores);

}// fetches scores of students in test

foreach($data as $key=> $grading) {

    foreach($scores as $key =>$values){



    if($values["marks"]>=$grading["grade_min"] && $values["marks"]<=$grading["grade_max"])

    print_r($values["marks"]);

    print_r($grading["grade"]);

    print_r($grading["remarks"]);

}

} Am trying to iterate each scores against the grading system but not successful, please help.


Array

(

    [0] => Array

        (

            [id] => 2

            [grade_min] => 1

            [grade_max] => 39

            [grade] => E

            [remarks] => Fail

        )


    [1] => Array

        (

            [id] => 3

            [grade_min] => 40

            [grade_max] => 49

            [grade] => D

            [remarks] => Pass

        )


    [2] => Array

        (

            [id] => 4

            [grade_min] => 50

            [grade_max] => 59

            [grade] => C

            [remarks] => Credit

        )


    [3] => Array

        (

            [id] => 5

            [grade_min] => 60

            [grade_max] => 69

            [grade] => B

            [remarks] => Good

        )


    [4] => Array

        (

            [id] => 6

            [grade_min] => 70

            [grade_max] => 79

            [grade] => A

            [remarks] => Very Good

        )


    [5] => Array

        (

            [id] => 7

            [grade_min] => 80

            [grade_max] => 100

            [grade] => A+

            [remarks] => Excellent

        )


)


我想根据评分系统数组迭代这些分数数组,并仅返回与分数介于“grade_min”和“grade_max”之间的“等级”和“备注”相匹配的“分数”


LEATH
浏览 98回答 1
1回答

拉莫斯之舞

最好迭代分数,然后迭代评分以找到分数所属的分数。此外,您的 if 没有大括号,因此它将仅执行它之后的第一个句子。// fetches the grading system whose array is seen belowwhile ($row = $grade->fetch(PDO::FETCH_ASSOC)) {&nbsp; &nbsp; $data = $row;&nbsp; &nbsp; // var_export($data); // No need to se this anymore}// fetches scores of students in testwhile($row = $stmt->fetch(PDO::FETCH_ASSOC)){&nbsp; &nbsp; $scores = $row;&nbsp; &nbsp; // var_export($scores); // No need to see this anymore}// Iterate scores first (I don't know if you need the index or not)foreach($scores as $scoreIndex => $score) {&nbsp; &nbsp; // Search grading for this score (Index not needed here, just values)&nbsp; &nbsp; foreach($data as $grading) {&nbsp; &nbsp; &nbsp; &nbsp; if($score['marks'] >= $grading['grade_min'] && $score['marks'] <= $grading['grade_max']) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Score is inside this grading&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Echo, print or assign what you need here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Then exit the loop for grading&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}根据 的输出,该数组只是一个值,没有关联数组,因此循环应为:$scores// Iterate scores first (I don't know if you need the index or not)foreach($scores as $scoreIndex => $score) {&nbsp; &nbsp; // Search grading for this score (Index not needed here, just values)&nbsp; &nbsp; foreach($data as $grading) {&nbsp; &nbsp; &nbsp; &nbsp; // $score is an integer, not an array&nbsp; &nbsp; &nbsp; &nbsp; if($score >= $grading['grade_min'] && $score <= $grading['grade_max']) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Score is inside this grading&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Echo here what you need&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Then exit the loop for grading&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP