【问题标题】:Using values in one associative array to test against values in another associative array使用一个关联数组中的值来测试另一个关联数组中的值
【发布时间】:2020-01-17 18:58:49
【问题描述】:

我需要帮助,尝试了几天都没有成功,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
        )

)
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
        )

)

分数数组如下所示:

Array
(
    [0] => 35
    [1] => 48
    [2] => 57
    [3] => 78
    [4] => 75
    [5] => 89
)

我想根据评分系统数组迭代这些分数数组,只返回匹配分数在“grade_min”和“grade_max”之间的“grade”和“remarks”

【问题讨论】:

  • 你已经尝试了什么?
  • 您能否var_export() 您的数组并将其发布在这里,这样我们就不必重新输入它们来摆弄它了?
  • @Triby,我已经编辑了我的问题以显示我的一次不成功的尝试。

标签: php if-statement foreach associative-array


【解决方案1】:

最好迭代分数,然后迭代评分以找到分数所属的分数。

另外,你的 if 没有花括号,所以它只会执行它之后的第一句。

// fetches the grading system whose array is seen below
while ($row = $grade->fetch(PDO::FETCH_ASSOC)) {
    $data = $row;
    // var_export($data); // No need to se this anymore
}

// fetches scores of students in test
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
    $scores = $row;
    // 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) {
    // Search grading for this score (Index not needed here, just values)
    foreach($data as $grading) {
        if($score['marks'] >= $grading['grade_min'] && $score['marks'] <= $grading['grade_max']) {
            // Score is inside this grading
            // Echo, print or assign what you need here

            // Then exit the loop for grading
            break;
        }
    }
}

根据您对$scores 的输出,该数组只是一个值,没有关联数组,因此循环应该是:

// Iterate scores first (I don't know if you need the index or not)
foreach($scores as $scoreIndex => $score) {
    // Search grading for this score (Index not needed here, just values)
    foreach($data as $grading) {
        // $score is an integer, not an array
        if($score >= $grading['grade_min'] && $score <= $grading['grade_max']) {
            // Score is inside this grading
            // Echo here what you need

            // Then exit the loop for grading
            break;
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-10
    • 2019-07-20
    • 1970-01-01
    • 2016-05-23
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    相关资源
    最近更新 更多