php array want to loop through for similar values

Posted on 2007-10-02
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


    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

    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 )

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

    Author Comment

    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 run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now