Anti-Moronic Posted May 25, 2009 Share Posted May 25, 2009 Is there a way to compare array values? Like: array("i am a string", "i am a zebra"); Would there be a way to compare both of them and give me a percentage on the comparison? I have no idea how to approach this. Any help is greatly appreciated. Link to comment https://forums.phpfreaks.com/topic/159616-array-question-compare-values/ Share on other sites More sharing options...
.josh Posted May 25, 2009 Share Posted May 25, 2009 levenshtein might be useful for you. Link to comment https://forums.phpfreaks.com/topic/159616-array-question-compare-values/#findComment-841868 Share on other sites More sharing options...
Anti-Moronic Posted May 25, 2009 Author Share Posted May 25, 2009 Wow, thanks...I searched for ages, couldn't come up with anything. Function looks extremely interesting. Thank you! Link to comment https://forums.phpfreaks.com/topic/159616-array-question-compare-values/#findComment-841870 Share on other sites More sharing options...
ToonMariner Posted May 25, 2009 Share Posted May 25, 2009 this can be intensive especially on large arrays... the functions to compare strings you will be interested are: http://uk3.php.net/manual/en/function.levenshtein.php and http://uk3.php.net/manual/en/function.similar-text.php if you needed to compare each string against every other string in your array then you would need to build the appropriate loop; something like <?php $strings = array("i am a string", "i am a zebra","i am not a string","i am a wildebeast","i am a thread","i am a matrix"); $string_no = count($strings); $compare = array(); for($i=0; $i<$string_no; $i++) { if ($i == ($string_no-1)) break; for($k=$i+1; $k<$string_no;$k++) { $compare['strings'][] = $i . ' - ' . $k; $compare['likeness'][] = levenshtein($strings[$i],$strings[$k]); } } print_r($compare); ?> Link to comment https://forums.phpfreaks.com/topic/159616-array-question-compare-values/#findComment-841872 Share on other sites More sharing options...
Anti-Moronic Posted May 25, 2009 Author Share Posted May 25, 2009 Cheers Toon, you just saved me even more time and confusion. Much appreciated. Link to comment https://forums.phpfreaks.com/topic/159616-array-question-compare-values/#findComment-841890 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.