Jump to content

array question (compare values)


Anti-Moronic

Recommended Posts

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.