从数组中删除重复值
即使我使用函数 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
确保返回的值实际上不是唯一的。例如
仍将包含 4 个条目。
如果任何条目包含空格,您应该
修剪
它们。Make sure that the returned values are in fact not unique. For example
Will still contain 4 entries.
If any entries contain spaces you should
trim
these.只需使用值作为键,它们只能存在一次,并且您没有任何数字作为关键字(希望如此):
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):
array_flip
will use values as keys (and drop duplicate values) andarray_keys
will then return all keys as values again.鉴于这是 aray_unique 应该做的唯一事情,我发现它没有这样做非常令人惊讶。从您的帖子中可以明显看出,也许您认为“php”与“PHP”是同一件事?
当我尝试以下操作时,我得到了独特的结果:
但是,问题的出现只是因为您的数据库架构未规范化。
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:
However the issue only arises because your database schema is not normalised.
由于似乎没有人提供正确的答案,我将在这里重复我的评论:
可能是这些单词前面或后面有空格。那么他们将永远不会彼此平等。您可以使用
trim
删除这些空格 [文档]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]