当有人在文本框中写入条目时,我想检查他们使用的单词并在我拥有的常用单词数据库中找到匹配项。数据库中的每个单词都有一个我需要它返回的数值。
我需要编写一个 if 语句来检查查询是否返回值,如果是,我需要它返回 -1。
这是我到目前为止所拥有的,但现在它只返回一个单词。
Entry::create($request->all());
// GETTING THE ARRY OF WORDS FROM entry_body( $entry_text)
$entry_text = $request->only('entry_body');
// REMOVE THE KEY AND JUST GET THE STRING VALUE
$entry_text = array_pull($entry_text,"entry_body");
$entry_noTag = strip_tags($entry_text);
// CONVERTE THE STRING ARRAY
$entry_explode = explode(" ", $entry_noTag);
// EACH WORD GETS ANALAYZED
$matched_words = Words::whereIn('word', $entry_explode)->get();
foreach ($entry_explode as $word) {
if ($matched_words == false){
return '-1';
}
else {
return $word;
}
};
我可以获得返回匹配值的代码。我需要它做的是,当数组中的单词不匹配时,它应该返回-1,这样我就知道在数据库中找不到该单词。
最佳答案
当然,您的第一次返回将返回并结束该函数。将所有值放入数组中,如下所示:
$arrayOfWords = array();
foreach ($matched_words as $word) {
if (in_array($word->word,$entry_explode)) {
$arrayOfWords['found'][] = $word->word;
} else {
$arrayOfWords['notFound'][] = $word->word;
}
}
return $arrayOfWords;
关于php - Laravel 5.2 数据库查询中的条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37079811/