0

I have a table with entries such as:

123 (DVD)

123 [DVD] [2007]

125 [2009]

189 (CD)

when I present these to the user in an autocomplete field I do away with anything between either () or [] as these are not relevant, but, as you can see from the list above, that leaves me with two entries for 123 which appear in the dropdown... is there anyway to further suppress duplicates? Sometimes there can be as many as 5 or 6 which looks wrong to say the least! Code below:

    // db select
    $query = "SELECT $title FROM PRprodINFO2 WHERE ((prodcatID = '$cat_id') AND ($title LIKE \"%" . $_GET["q"] . "%\")) group by $title LIMIT 8"; 

$result = mysql_query($query);

$output_items = array();

// while loop to print results
    while($row = mysql_fetch_array($result)) { $output_items[] = $row[$title]; } 

$output_items = preg_replace('/\[.*?\]|\s*/', '', $output_items); // remove [blah]

$output_items = preg_replace('/\(.*?\)|\s*/', '', $output_items); // remove (blah)

    print(implode("\n", $output_items));

Many thanks

Leaurus
  • 376
  • 3
  • 13
StudioTime
  • 22,603
  • 38
  • 120
  • 207

2 Answers2

1

The array_unique() function removes duplicate values from an array, in your case the $output_items array.

Take a look at http://php.net/manual/en/function.array-unique.php

Logan Serman
  • 29,447
  • 27
  • 102
  • 141
0

You can use array_unique ( array $array ) to remove all duplicates before imploding.

Sylverdrag
  • 8,898
  • 5
  • 37
  • 54