我有以下代码:
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>";
}结果是:
不适用
不适用
不适用
不适用
不适用
我的期望值如下:
找到
找到
找到
不适用
不适用
并成功地使用了这一守则:
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";
}那么,我能做些什么来循环其他代码,如果并以其他代码结束,就像我上面的成功代码一样?
谢谢你的帮助
发布于 2019-02-14 10:00:01
当您在循环中覆盖$ai_cat_detail的值时,您的输出出现了错误--所以最后一个赋值是N/A (所以只有在找到最后一个if时,它才会回显FOUND。
为了修复这个问题,将检查导出到函数并返回字符串值,或者使用断开作为:
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>";
}https://stackoverflow.com/questions/54682652
复制相似问题