大家好我有一个问题。我有一个动态填充的数组。在数组中有2种主要类型的项目。文件名等于27个字符的项目,其余的或多或少。我能分开两种类型。第二个列表被添加到名为$ usedArray的新数组中。然后迭代这些项目,文件名从字符0,6继承,用于比较页面上的最终用户输入。 如果在该数组中找到该项,它将触发一个函数,向它们发送文本和完整文件名的电子邮件。我的问题是,如果在x次迭代之前找不到该项,它将触发未找到的函数x次数,如果它根本找不到它会做同样的事情。如果我有99件不匹配的物品会发射99次。停止射击我离开了未找到只是在屏幕上找不到的打印。我想在循环外部调用notfound函数,但是如果找到了一个项目则不希望它被触发 这是我到目前为止的代码
do{
if (substr($val,0,6) == $studentID)
{
$codeFound = substr($val,22,19);
print_r($studentID . ' is found <br /> Their code is ' . $codeFound);
//sendText($phoneNum,$codeFound,$messageMonth);
//sendEmail($emailInfo,$messageMonth,$codeFound);
break 1;
}
else
{
print_r($studentID . " was not found <br />");
}
} while(list(, $val) = each($usedArray));
这是我的输出
166003 was not found
166003 was not found
166003 was not found
166003 is found
Their code is xxxxxxxxxx
答案 0 :(得分:1)
我认为你应该添加一个标志来跟踪你是否找到了什么:
$item_found = false;
do{
if (substr($val,0,6) == $studentID)
{
$codeFound = substr($val,22,19);
print_r($studentID . ' is found <br /> Their code is ' . $codeFound);
//sendText($phoneNum,$codeFound,$messageMonth);
//sendEmail($emailInfo,$messageMonth,$codeFound);
// item found!
$item_found = true;
break 1;
}
} while(list(, $val) = each($usedArray));
// now check - if `$item_found` is false
// then you can send your NotFoundEmail
if (!$item_found) {
sendNotFoundEmail();
}