Home > Enterprise >  Delete elements of an php array in Laravel
Delete elements of an php array in Laravel

Time:10-17

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", "winter"];

But I need to store in $finalArray also the words that are duplicated in $textArray like this:

$finalArray = ["four", "winter", "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
         }
    }

But I don't know how to do that. Can someone help me with this please

CodePudding user response:

You can count the looped string/word appear inside $textArray using array_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.

$finalArray = [];
    
for($i=0; $i<count($textArray); $i  ){
    if(!in_array($textArray[$i], $correctArray)){
        array_push($finalArray, $textArray[$i]);
    }

    // count how many times the string appear in the array
    $total_string_in_array = array_count_values($textArray)[$textArray[$i]];
    if ($total_string_in_array > 1) {
        // check if the duplicate string already exists inside $finalArray
        if ( ! in_array($textArray[$i], $finalArray)) {
            array_push($finalArray, $textArray[$i]);
        }
    }
}

Reference:

  • Related