使用 PHP 重复数据删除 mysql 结果
我有一个表格,其中包含以下条目:
123 (DVD)
123 [DVD] [2007]
125 [2009]
189 (CD)
当我在自动完成字段中向用户呈现这些内容时,我会删除 () 或 [] 之间的任何内容,因为这些内容不相关,但是,正如您从上面的列表中看到的那样,留下了两个 123 的条目,它们出现在下拉列表中......有没有办法进一步抑制重复项?有时可能有多达 5 或 6 个,至少可以说看起来是错误的!代码如下:
// 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));
非常感谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
array_unique()
函数从数组中删除重复值,在您的例子中是$output_items
数组。看看 http://php.net/manual/en/function.array -unique.php
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
您可以使用 array_unique ( array $array ) 在内爆之前删除所有重复项。
You can use
array_unique ( array $array )
to remove all duplicates before imploding.