2

I have been trying to use array_unique to remove duplicates from the search results I am populating. However, I am getting an undefined offset error. I was wondering if anyone knows what is causing this error and what I can do to fix it? As you can see below I am populating search results from two different places and trying to merge all matches into one array.

Before array_unique: Array ( [0] => Bob Marley Footwear [1] => Bob Marley Footwear [2] => DVS Shoe Co. )

After array_unique: Array ( [0] => Bob Marley Footwear [2] => DVS Shoe Co.)

Note: this causes an error message to appear when trying to access array slot 1 when iterating through array, to print out the results.

    $results = mysql_query("SELECT keywords,name FROM files WHERE MATCH (keywords,name) AGAINST ('$searchfor')") or die(mysql_error());


    $matches=Array();
    $matches_final=Array();

    while($row = mysql_fetch_array($results)){
        $matches[] = $row['name'];
    }
    $matches_final = array_unique($matches);

    $results1 = mysql_query("SELECT name FROM files");
    while($row1 = mysql_fetch_array($results1)){
        $temp = explode(" ", $row1['name']);

        for($z=0; $z < sizeof($search_words); $z++){
            for($i=0; $i < sizeof($temp); $i++){
                if(((strcmp(strtolower($search_words[$z]), strtolower($temp[$i])))==0) and strlen($search_words[$z])<=5){
                    $matches_final[] = $row1['name'];
                }
            }
        }
    }


return $matches_final;
2
  • I might be tired, but I can't see anything that would error there. Which line is throwing the error? No possibility for using SQL's distinct here to ensure uniqueness that way? Commented Jun 30, 2012 at 23:47
  • the error is thrown when trying to iterate through the array to print out the array slot 1 which was removed due to using the unique_array function. Commented Jun 30, 2012 at 23:51

1 Answer 1

6

I'd like to first suggest you use DISTINCT(name) in your query to remove the need for array_unique.

The error is due to array_unique removing an element and not rekeying the array. You could perform array_values on your array prior to returning it to rekey.

Sign up to request clarification or add additional context in comments.

2 Comments

Ah yes of course, well spotted. array_unique preserves keys, and, unlike some functions, has no option to change this behaviour.
thanks for the help! array_values did the trick! Using mySQL DISTINCT would not work because there aren't any duplicates in my DB. The duplicates occur while populating the '$matches' array from more than one source, to cater to many different search queries, b/c mysql full-text search leaves out some search results. Appreciate the help very much:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.