1

我有以下代码:

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>";
}

结果是:

不适用 不适用 不适用
不 适用

适用

我的期望值是这样的:
Found
Found
Found
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";
}

那么我能做些什么来循环一个 else if 并以上面的成功代码之类的代码结尾呢?

感谢帮助

4

1 回答 1

0

当您覆盖$ai_cat_detail循环中的值时,您的输出错误 - 所以最后一个分配是N/A您回显的那个(因此FOUND只有在找到最后一个 if 时才会回显。

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

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>";
}
于 2019-02-14T10:00:01.487 回答