I have two strings and I need to compare the words of the each sentence.
The strings are this two:
$correctSentence = "The year stations are summer, winter, spring and fall.";
$textSentence = "The year stations are four summer, winter and spring and summer.";
So, I make arrays of each sentence and I have the next arrays to compare the words who are no similar between sentences.
$correctArray = ["The", "year", "stations", "are", "summer", "winter", "spring", "and", "fall"];
$textArray = ["The", "year", "stations", "are", "four", "summer", "winter", "and", "spring", "and", "summer"];
I’m comparing the arrays in this form:
With a for loop, I compare every word of the $textArray with the words of $correctArray
.
And if the word of $textArray[$i]
is not similar to any word of $correctArray
, that word is stored in a new array called $finalArray
.
$finalArray = [];
for($i=0; $i<count($textArray); $i++){
if(!in_array($textArray[$i], $correctArray)){
array_push($finalArray, $textArray[$i]);
}
}
The results of $finalArray are this:
$finalArray = ["four"];
But I need to store in $finalArray
also the words that are duplicated in $textArray
like this:
$finalArray = ["four", "and", "summer"];
Because "and" and "summer" are duplicated in the $textSentence
and $textArray
, so that words don’t are similar to the words of $correctSentence
and $correctArray
I’m thinking the solution is delete the similar word of $correctArray
when the if condition is true in this section:
$finalArray = [];
for($i=0; $i<count($textArray); $i++){
if(!in_array($textArray[$i], $correctArray)){
array_push($finalArray, $textArray[$i]);
//In this section, delete the word of $correctArray
//which is similar of $textArray[$i].
//For example: After comparing "summer" of $textArray[$i] with "summer" of $correctArray.
//The record "summer" of $correctArray should be deleted.
//And this way the following record "summer" of $textArray (which is duplicated)
//should be stored in $finalArray
}
}
2
Answers
You can count the looped string/word appear inside
$textArray
usingarray_count_values()
and check if the counted value is more than 1 (means duplicate) than insert into$finalArray
, also maybe before insert to$finalArray
you need to check if the string/word already exists inside$finalArray
or not.Reference:
PHP has native functions for everything that you need for this task.
str_word_count()
.array_diff()
.array_count_values()
.array_filter()
.array_keys()
.array_merge()
.Code: (Demo)