0

I want to compare array of words with a field of database table which contains more than 2000 entries. For example user input a string "this is good" It will explode the string into array and than match the words with the database table , such as good exists in the table then it will return the good keyword. here is my code

$cmnt = $request['comment'];
$parse = explode(' ',$cmnt);
$length = count($parse);
for($i = 0; $i < $length ; $i++)
{
    $keyword = Rate::where('keyword',$parse[$i]);
}
if ($keyword != null)
{
    dd($keyword);
}

Here is my out put My output is displaying the keyword like this in the picture

1 Answer 1

3

You are over writing value of $keyword in each loop iteration do like this

$cmnt = $request['comment'];
$parse = explode(' ',$cmnt);
$parse = array_unique($parse);
$keyword = Rate::whereIn('keyword',$parse);
dd($keyword);
Sign up to request clarification or add additional context in comments.

2 Comments

But I want to compare all the values of array thats why I am using for loop , how this can work without iterations?
the above will create a query like select * from rate where keyword in('this','is', 'good').

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.