如何将similar_text应用于array_uintersect?

时间:2011-06-18 20:02:26

标签: php arrays function similarity sorting

此问题与Francois Deschenes's answer之前的问题有关。

我不确定如何将我的文本相似性检查功能应用于array_uintersect功能。

这是我的功能(打开改进的想法):

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

1 个答案:

答案 0 :(得分:1)

嗯,这可能是我所说的傻,我不知道,但我不明白为什么你有一个带有这些键的数组(值,如你的另一篇文章中所示)。我不确定你要做什么,我只是猜测你只需要一个可能有2个维度的数组(只是一个猜测,不确定):

<?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 ;

?>