亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用一個關聯數組中的值來測試另一個關聯數組中的值

使用一個關聯數組中的值來測試另一個關聯數組中的值

PHP
LEATH 2022-08-05 16:40:32
我需要幫助,嘗試了好幾天沒有成功,PHP新手,所以請原諒我,我有一個從數據庫表評分系統返回的關聯數組,我想實現的是嘗試來自另一個關聯數組的“分數”,迭代評分系統,直到我找到一個分數,該分數落在評分系統中連續的值之間,然后返回字母等級和備注, 看到下面已經嘗試過什么,我筋疲力盡,任何幫助將不勝感激。我嘗試過的代碼while ($row = $grade->fetch(PDO::FETCH_ASSOC)) {        $data = $row;        var_export($data);    } //fetches the grading system whose array is seen belowwhile($row = $stmt->fetch(PDO::FETCH_ASSOC)){    $scores = $row;    var_export($scores);}// fetches scores of students in testforeach($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”之間的“等級”和“備注”相匹配的“分數”
查看完整描述

1 回答

?
拉莫斯之舞

TA貢獻1820條經驗 獲得超10個贊

最好迭代分數,然后迭代評分以找到分數所屬的分數。


此外,您的 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;

        }

    }

}


查看完整回答
反對 回復 2022-08-05
  • 1 回答
  • 0 關注
  • 116 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號