使用LOOP IF,ELSEIF等,从数据库中找到字符lindost



我有以下代码:

for ($y = 0; $y <= $count_1; $y++) {
    for ($x = 0; $x <= $count_2; $x++) {
        if((strpos($cat[$y],"Model 1")!==false)and (stripos($quest[$y],$search_quest[$x])!==false) and (stripos($answ[$y],$search_answ[$x])!== false)) { 
            $ai_cat_detail ="FOUND";
        } else {
            $ai_cat_detail ="N/A";
        }
    }
    echo $ai_cat_detail."<br>";
}

结果是:

n/a
n/a
n/a
n/a
N/A

我是这样的预期价值:
找到
找到
找到
n/a
N/A

和此代码的成功:

if((strpos($cat[$y],"Model 1")!==false)and(stripos($quest[$y],"Search Quest 1")!==false) and (stripos($answ[$y],"Search Answer 1")!== false)) {     
    $ai_cat_detail = "FOUND";
} elseif((strpos($cat[$y],"Model 1")!==false)and(stripos($quest[$y],"Search Quest 2")!==false) and (stripos($answ[$y],"Search Answer 2")!== false)){ 
    $ai_cat_detail = "FOUND";
} elseif((strpos($cat[$y],"Model 1")!==false)and (stripos($quest[$y],"Search Quest 3")!==false) and (stripos($answ[$y],"Search Answer 3")!== false)) { 
    $ai_cat_detail = "FOUND";
} elseif((strpos($cat[$y],"Model 1")!==false)and (stripos($quest[$y],"Search Quest 4")!==false) and (stripos($answ[$y],"Search Answer 4")!== false)) { 
    $ai_cat_detail = "FOUND";
} else { 
    $ai_cat_detail = "N/A";
}

那么,如果我的成功代码像上面的成功代码一样,我该怎么办?

以其他代码结尾?

感谢您的帮助

当您覆盖循环中 $ai_cat_detail的值时,您的输出错误 - 因此,最后一个分配是 N/A是您回声的(因此,仅在最后一个时,它才会回声FOUND找到。

为了修复该检查以功能的导出并返回字符串值或使用 break as:

for ($y = 0; $y <= $count_1; $y++) {
    for ($x = 0; $x <= $count_2; $x++) {
        if((strpos($cat[$y],"Model 1") !== false) and (stripos($quest[$y],$search_quest[$x]) !== false) and (stripos($answ[$y],$search_answ[$x]) !== false)) { 
            $ai_cat_detail ="FOUND";
            break; // this will stop the loop if founded
        } else {
            $ai_cat_detail ="N/A";
        }
    }
    echo $ai_cat_detail."<br>";
}

或使用函数为:

function existIn($cat, $search_quest, $search_answ, $count_2, $y) {
    for ($x = 0; $x <= $count_2; $x++) {
        if((strpos($cat[$y],"Model 1") !== false) and (stripos($quest[$y],$search_quest[$x]) !== false) and (stripos($answ[$y],$search_answ[$x]) !== false)) { 
            return "FOUND";
        }
    }
    return "N/A";
//use as
for ($y = 0; $y <= $count_1; $y++) {
    echo existIn($cat, $search_quest, $search_answ, $count_2, $y) ."<br>";
}

相关内容

  • 没有找到相关文章

最新更新