djinnov8 Posted September 12, 2010 Share Posted September 12, 2010 Hi guys, I am new to PHP/coding and am trying to look for 1. A way of comparing the words in one static array against other dynamically created arrays (created from mysql queries) 2. Work out how many similar words there are - then assign that number to that array My static array is...$comparewithme = array with values = this is an example of an array Mysql_query("select id, words from table_example") Results from query are put into an array that is named according to id.. $result2 = array with values = this is an example of queries $result3 = array with values = this is not an example of php Comparison should give the following info Comparing $comparewithme with $result2 should generate a hit rate of 5 (similar words=this is an example of)... Comparing $comparewithme with $result3 should generate a hit rate of 4 (similar words=this is an example)... Any ideas greatly appreciated...thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/213207-compare-words-in-two-arrays-and-calculate-number-of-hits/ Share on other sites More sharing options...
djinnov8 Posted September 12, 2010 Author Share Posted September 12, 2010 I came across a post that contained something kinda similar... http://stackoverflow.com/questions/1957989/compare-5000-strings-with-php-levenshtein Would Levenshtein do this and if so, I wonder what the syntax would look like? Quote Link to comment https://forums.phpfreaks.com/topic/213207-compare-words-in-two-arrays-and-calculate-number-of-hits/#findComment-1110183 Share on other sites More sharing options...
djinnov8 Posted September 12, 2010 Author Share Posted September 12, 2010 Found this at ... http://php.tonnikala.org/manual/cs/function.levenshtein.php june05 at tilo-hauke dot de 06-Jun-2005 10:44 //levenshtein for arrays function array_levenshtein($array1,$array2) { $aliases= array_flip(array_values(array_unique(array_merge($array1,$array2)))); if(count($aliases)>255) return -1; $stringA=''; $stringB=''; foreach($array1 as $entry) $stringA.=chr($aliases[$entry]); foreach($array2 as $entry) $stringB.=chr($aliases[$entry]); return levenshtein($stringA,$stringB); } // e.g. use array_levenshtein to detect special expressions in unser-inputs echo array_levenshtein(split(" ", "my name is xxx"), split(" ","my name is levenshtein")); //output: 1 Quote Link to comment https://forums.phpfreaks.com/topic/213207-compare-words-in-two-arrays-and-calculate-number-of-hits/#findComment-1110185 Share on other sites More sharing options...
ignace Posted September 12, 2010 Share Posted September 12, 2010 What you are trying to do is already built-in to MySQL SELECT *, MATCH(words) AGAINST($query) AS relevancy FROM table_example WHERE MATCH(words) AGAINST($query); Quote Link to comment https://forums.phpfreaks.com/topic/213207-compare-words-in-two-arrays-and-calculate-number-of-hits/#findComment-1110197 Share on other sites More sharing options...
djinnov8 Posted September 12, 2010 Author Share Posted September 12, 2010 What you are trying to do is already built-in to MySQL SELECT *, MATCH(words) AGAINST($query) AS relevancy FROM table_example WHERE MATCH(words) AGAINST($query); Thanks Ignace...You're truly the king!!! Quote Link to comment https://forums.phpfreaks.com/topic/213207-compare-words-in-two-arrays-and-calculate-number-of-hits/#findComment-1110229 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.