[Last Call] Learn how to a build a cloud-first strategyRegister Now


php array want to loop through for similar values

Posted on 2007-10-02
Medium Priority
Last Modified: 2012-05-05
I am trying to merge two tables of similar data, but am having the problem that the indexes are text, and dont match in both tables. For example "This Weeks Advertisements" might be "This weeks Advertisement" in the other.

So far I have put all the values from both tables into an array and am comparing them by eye but its taking a long time.

My question is, is there an easy way to search through an array for similar values and group them based on some kind of score that indicates how similar they are?
Question by:daveboyle99
LVL 13

Assisted Solution

MasonWolf earned 400 total points
ID: 20002954
Sure. http://us.php.net/manual/en/function.similar-text.php

If the reference on its own isn't enough to get you your results, just explain how you're merging the two tables and I can write a snippet of code that should give you what you want.
LVL 34

Accepted Solution

Beverley Portlock earned 600 total points
ID: 20004795
If you have them in arrays then do this.

1. Convert them all to lower case. (use strtolower)
2. Remove punctuation (use str_replace( array(".", ",", "'", "!", "?"),"", $anArray[$field] );
3. Replace multiple spaces with a single space (again str_replace( array("  ", "   ", "    ", "     "), "", $anArray[$field] )
4. Sort into sequence
5. For each pair calculate the levenshtein distance  (see http://www.php.net/levenshtein )

As Masonwolf says, without any code snippets that's about all the advice we can give.

Author Comment

ID: 20009029
Thanks for the great answers guys. I solved with the folowing (rather clumsy yet effective) code :

foreach ($total_names as $r) {
foreach ($total_names as $s) {
$lev = levenshtein($r, $s);
if (($lev<2) && ($lev!=0)) {
echo "$r (Input) <br>$s (Word) $lev (Score)<br><br>\n";

n.b set_time_limit(0); doesnt always work, use metabase explorer to set CGI_TIMEOUT :)

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses
Course of the Month18 days, 13 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question