简体   繁体   English

array_unique无法正常工作?

[英]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 eg 已经在查询中使用了“ by by”或“ group by”,但是,数据为每个主题提取了几条记录,然后我将其精简,实际上使它们变得不唯一,例如

I may be looking at a table of DVD titles and return these results: 我可能正在看一张DVD标题表并返回以下结果:

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: 我只想向用户显示标题,因此我将第一个非字母/数字字符之后的所有内容都剥离,然后将trim()剥离,从而使Goodfellas这个单词的3个实例成为以下代码:

$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();

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));

只需使用:

$array = array_unique($array, SORT_REGULAR);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM