Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php in_数组方法只运行一次?_Php - Fatal编程技术网

Php in_数组方法只运行一次?

Php in_数组方法只运行一次?,php,Php,我试图创建一个函数来检查输入的单词是否已经在我的数据库中 我正在使用以下代码: public function contains($string, $pieces) { $this->getWords(); $arr = $this->_data; // Get everything from DB $words = array(); $foundWords = array(); foreach($arr as $key) {

我试图创建一个函数来检查输入的单词是否已经在我的数据库中

我正在使用以下代码:

public function contains($string, $pieces)
{
    $this->getWords();
    $arr = $this->_data; // Get everything from DB

    $words = array();
    $foundWords = array();
    foreach($arr as $key)
    {
        $words[] = $key->word; // Put words in array
    }

    foreach($words as $item)
    {
        if (in_array($item, $words))
        { // Check if entered words are in the DB array

            $foundWords[] = $item; // Put already existing words in array

            print_r($foundWords);

            echo "Match found: ";
            echo $item . '<br>'; // Echo found words

            return true;
        }
        echo "Not found!";
        return false;
    }
}
公共函数包含($string,$pieces)
{
$this->getWords();
$arr=$this->\u data;//从数据库获取所有信息
$words=array();
$foundWords=array();
foreach($arr作为$key)
{
$words[]=$key->word;//将单词放入数组
}
foreach($words作为$item)
{
if(在_数组中($item,$words))
{//检查输入的字是否在数据库数组中
$foundWords[]=$item;//将已存在的字放入数组中
打印(文字);
回声“匹配发现:”;
echo$item。“
”;//echo找到单词 返回true; } 回声“找不到!”; 返回false; } }
问题是,它只检查输入单词数组中的第一个单词。 有人知道为什么会这样吗

示例:

用户输入
“这是一个测试”
。 数据库包含以下文字:
This,is,a,test

代码的输出现在应该是
Match found:This,is,a,test
,并且
$foundWords
数组应该包含这些单词


相反,它只查找第一个单词,
$foundWords
数组只包含第一个单词。因此,
Match found:This

从函数中为返回状态创建一个变量。这里我使用
$flag
变量来检查单词的状态。默认情况下,我将变量设置为false,如果找到单词,则从循环中将变量设置为
true
break
,并返回
标志

public function contains($string, $pieces){
    $flag = false;
    $this->getWords();
    $arr = $this->_data; // Get everything from DB

    $words = array();
    $foundWords = array();
    foreach($arr as $key) {
        $words[] = $key->word; // Put words in array
    }

    foreach($words as $item) {
        if (in_array($item, $words)) { // Check if entered words are in the DB array

            $foundWords[] = $item; // Put already existing words in array

            print_r($foundWords);

            echo "Match found: ";
            echo $item . '<br>'; // Echo found words

            $flag = true; //set true and break from the loop
            break;
        }else{
            echo "Not found!";
            $flag = false;
        }               
    }
    return $flag;
}
公共函数包含($string,$pieces){
$flag=false;
$this->getWords();
$arr=$this->\u data;//从数据库获取所有信息
$words=array();
$foundWords=array();
foreach($arr作为$key){
$words[]=$key->word;//将单词放入数组
}
foreach($words作为$item){
如果(在数组中($item,$words)){//检查输入的单词是否在数据库数组中
$foundWords[]=$item;//将已存在的字放入数组中
打印(文字);
回声“匹配发现:”;
echo$item。“
”;//echo找到单词 $flag=true;//设置true并从循环中断 打破 }否则{ 回声“找不到!”; $flag=false; } } 返回$flag; }
删除“返回”并检查是否找到任何单词:

 foreach($words as $item) {
        if (in_array($item, $words)) { // Check if entered words are in the DB array

            $foundWords[] = $item; // Put already existing words in array
        }
    }
  if(count($foundWords) == 0) {

        echo "Not found!";
  }
  else
  {  
            echo "Words: ";
            print_r($foundWords);
  }

您确实知道
return
在函数/方法中的作用,不是吗?您需要在此处和函数返回值的最后一行使用
break
语句。@MarkBaker感谢您通知我这一点。我完全忘记了它停止了功能。这就解决了!