从数组中删除重复值

发布于 2024-11-25 08:58:17 字数 1045 浏览 2 评论 0原文

即使我使用函数 array_unique,我也无法从数组中“删除”双精度值!

<?php
    $tags_array = array() ; 
    $query_tags = $mysql->Query("SELECT words AS kw FROM users ") ;
    /****
    *
    * This query return something like Array([1] => PHP,ASP,NET [2] => Ruby,Jquery,php,asp_net [3] => Php,asp,visualbasic,c# [4] => [5] =>)
    *
    *****/
    while($fetch_tags = $mysql->Fetch($query_tags)) 
    {
        foreach(explode(',',$fetch_tags['kw']) as $kw => $value) 
        {
            if(empty($value)) ;
            else 
            {
                $value = ucwords(strtolower($value)) ;
                $tags_array[] = $value ;
            } 
        }
    }
$tags_array = array_values(array_unique($tags_array, SORT_STRING)) ; 
print_r($tags_array) ;
/******
*
* print_r show somethings like this Array([1] => Asp [2] => Php [3] => Php [4] => Ruby [5] => Jquery [6] => Php [7] => Asp_net [8] = >C# [9] => Asp) 
*
* IT'S ONLY AN EXAMPLE TO SHOW YOU THE SITUATION 
*****/
?>

I can not "remove" the double values ​​from an array even if I use the function array_unique!

<?php
    $tags_array = array() ; 
    $query_tags = $mysql->Query("SELECT words AS kw FROM users ") ;
    /****
    *
    * This query return something like Array([1] => PHP,ASP,NET [2] => Ruby,Jquery,php,asp_net [3] => Php,asp,visualbasic,c# [4] => [5] =>)
    *
    *****/
    while($fetch_tags = $mysql->Fetch($query_tags)) 
    {
        foreach(explode(',',$fetch_tags['kw']) as $kw => $value) 
        {
            if(empty($value)) ;
            else 
            {
                $value = ucwords(strtolower($value)) ;
                $tags_array[] = $value ;
            } 
        }
    }
$tags_array = array_values(array_unique($tags_array, SORT_STRING)) ; 
print_r($tags_array) ;
/******
*
* print_r show somethings like this Array([1] => Asp [2] => Php [3] => Php [4] => Ruby [5] => Jquery [6] => Php [7] => Asp_net [8] = >C# [9] => Asp) 
*
* IT'S ONLY AN EXAMPLE TO SHOW YOU THE SITUATION 
*****/
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

擦肩而过的背影 2024-12-02 08:58:17

确保返回的值实际上不是唯一的。例如

$foo = array("PHP","php","pHP","PHP ");
$foo = array_unique($foo);

仍将包含 4 个条目。

如果任何条目包含空格,您应该修剪它们。

Make sure that the returned values are in fact not unique. For example

$foo = array("PHP","php","pHP","PHP ");
$foo = array_unique($foo);

Will still contain 4 entries.

If any entries contain spaces you should trim these.

何以畏孤独 2024-12-02 08:58:17

只需使用值作为键,它们只能存在一次,并且您没有任何数字作为关键字(希望如此):

$tags_array = array_keys(array_flip(($tags_array));

array_flip 将使用值作为键(并删除重复值)和 array_keys 然后将再次返回所有键作为值。

Just use values as keys, they can only exist once and you don't have any numbers as your keywords (hopefully):

$tags_array = array_keys(array_flip(($tags_array));

array_flip will use values as keys (and drop duplicate values) and array_keys will then return all keys as values again.

七月上 2024-12-02 08:58:17

鉴于这是 aray_unique 应该做的唯一事情,我发现它没有这样做非常令人惊讶。从您的帖子中可以明显看出,也许您认为“php”与“PHP”是同一件事?

当我尝试以下操作时,我得到了独特的结果:

$d=Array('PHP,ASP,NET','Ruby,Jquery,php,asp_net','Php,asp,visualbasic,c#');

$o=array();

foreach ($d as $i) {
    $p=explode(',',$i);
    foreach ($p as $q) {
      $o[]=strtoupper($q);
    }
}
print_r(array_unique($o));

但是,问题的出现只是因为您的数据库架构未规范化。

Given that is the only thing that aray_unique is supposed to do, I find it very surprising it's not doing it. What is apparent from your post, is that maybe you think 'php' is the same thing as 'PHP'?

When I try the following I get unique results:

$d=Array('PHP,ASP,NET','Ruby,Jquery,php,asp_net','Php,asp,visualbasic,c#');

$o=array();

foreach ($d as $i) {
    $p=explode(',',$i);
    foreach ($p as $q) {
      $o[]=strtoupper($q);
    }
}
print_r(array_unique($o));

However the issue only arises because your database schema is not normalised.

败给现实 2024-12-02 08:58:17

由于似乎没有人提供正确的答案,我将在这里重复我的评论:

可能是这些单词前面或后面有空格。那么他们将永远不会彼此平等。您可以使用 trim 删除这些空格 [文档]

$value = ucwords(strtolower(trim($value)));

As no one seemed to have provided the right answer, I'll repeat my comment here:

It might be that the words have preceding or trailing white spaces. Then they will never be equal to each other. You can remove these white spaces with trim [docs]

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