使用 PHP 重复数据删除 mysql 结果

发布于 2024-12-19 15:11:26 字数 842 浏览 2 评论 0原文

我有一个表格,其中包含以下条目:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦初启 2024-12-26 15:11:26

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

堇年纸鸢 2024-12-26 15:11:26

您可以使用 array_unique ( array $array ) 在内爆之前删除所有重复项。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文