StudioTime
StudioTime

Reputation: 23979

array_unique not working?

I have the following code, but, array unique is simply not working and i have no idea why?

A group by or distinct on the query is already used, but, the data pulls several records for each subject which I then strip down and in effect make them non unique e.g.

I may be looking at a table of DVD titles and return these results:

Goodfellas - 2010 remaster
Goodfellas (2006)
Goodfellas: 2011 remastered remaster of a remaster from last year

I only want to show the user the title so I strip everything after the first non letter/number character, then trim(), thus making 3 instances of the word Goodfellas here is the code:

$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($row = mysql_fetch_array($result)) { 

$output_items[] = $row[$title]; 

} // end while

// remove non characters
$output_items = preg_replace('/[^\w\s].*$/', "", $output_items); 

$output = array_unique($output_items);

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

mysql_close();

Upvotes: 1

Views: 1877

Answers (2)

iniravpatel
iniravpatel

Reputation: 1643

Simply use:

$array = array_unique($array, SORT_REGULAR);

Upvotes: 2

Diego Agulló
Diego Agulló

Reputation: 9576

You are not trimming them! Try that way:

$output_items = preg_replace('/[^\w\s].*$/', "", $output_items);
$output_items = array_map('trim', $output_items);

$output = array_unique($output_items);

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

Upvotes: 3

Related Questions