Search code examples
phparraysfunctionsimilaritysorting

How to apply similar_text to array_uintersect?


This question relates to Francois Deschenes's answer to one of my previous questions.

I am unsure how to apply my text similarity check function to the array_uintersect function.

Here is my function (Open for ideas on improvement):

function checkSimilar($str1, $str2){
    similar_text($str1, $str2, $percent);
    if($percent > 75){ 
        return $str2; 
    }
    else{ 
        return null;
    }
}

Solution

  • Well this might be silly what I am saying, I don't know, but I don't understand why you have an array with such keys (values, as shown in your other post). Not really sure what you are trying to do, I just made a guess that you need only one array with maybe 2 dimensions (just a guess, not sure):

    <?php
    
     $song[]=array('title'=>'boing', 'singer'=>'john smith', 'year'=>'1949');
     $song[]=array('title'=>'Don\'t Trust me', 'singer'=>'3oh!3', 'year'=>'1929');
     $song[]=array('title'=>'You Belong with me', 'singer'=>'Taylor Swift', 'year'=>'1981');
     $song[]=array('title'=>'You Belong to earth', 'singer'=>'Taylor Swift', 'year'=>'1991');
     $song[]=array('title'=>'You Do  Belong Everywhere', 'singer'=>'Taylor Swift', 'year'=>'1971');
     $song[]=array('title'=>'Fire Burning', 'singer'=>'Sean Kingston', 'year'=>'2010');
     $song[]=array('title'=>'Love Your Enemy', 'singer'=>'Green Day', 'year'=>'1997');
     $song[]=array('title'=>'Gone', 'singer'=>'Kelly Clarkson', 'year'=>'1956');
     $song[]=array('title'=>'Know Your Enemy', 'singer'=>'Green Day', 'year'=>'1997');
     $song[]=array('title'=>'Gone long away', 'singer'=>'Kelly Clarkson', 'year'=>'1976');
    
     $find='belong me';
     $accepted=15; 
     $tmp=array();
    
     foreach($song as  $key => $value)
     { 
         similar_text(strtoupper($value['title']),  strtoupper($find), $p);
         if($p>=$accepted)
         $tmp[$p][] = 'title: '.$value['title'].' | rate: '.round($p,2).'%<br>'; 
     }
     krsort($tmp);
    
      echo 'THIS IS YOUR SEARCH RESULT FOR \'',$find,'\':<br>';
      echo '_____________________________________________________<br>';
      foreach($tmp as $key => $percentage)
        foreach($percentage as $value)
            echo $value ;
    
    ?>