PHP:在数组的数组上使用 array_unique?
我有一个数组,
Array(
[0] => Array
(
[0] => 33
[user_id] => 33
[1] => 3
[frame_id] => 3
)
[1] => Array
(
[0] => 33
[user_id] => 33
[1] => 3
[frame_id] => 3
)
[2] => Array
(
[0] => 33
[user_id] => 33
[1] => 8
[frame_id] => 8
)
[3] => Array
(
[0] => 33
[user_id] => 33
[1] => 3
[frame_id] => 3
)
[4] => Array
(
[0] => 33
[user_id] => 33
[1] => 3
[frame_id] => 3
)
)
如您所见,键 0 与 1、3 和 4 相同。键 2 与它们都不同。
当在它们上运行 array_unique 函数时,唯一剩下的是“
Array (
[0] => Array
(
[0] => 33
[user_id] => 33
[1] => 3
[frame_id] => 3
)
)
有什么想法为什么 array_unique 不能按预期工作吗?”
I have an array
Array(
[0] => Array
(
[0] => 33
[user_id] => 33
[1] => 3
[frame_id] => 3
)
[1] => Array
(
[0] => 33
[user_id] => 33
[1] => 3
[frame_id] => 3
)
[2] => Array
(
[0] => 33
[user_id] => 33
[1] => 8
[frame_id] => 8
)
[3] => Array
(
[0] => 33
[user_id] => 33
[1] => 3
[frame_id] => 3
)
[4] => Array
(
[0] => 33
[user_id] => 33
[1] => 3
[frame_id] => 3
)
)
As you can see key 0 is the same as 1, 3 and 4. And key 2 is different from them all.
When running the array_unique function on them, the only left is
Array (
[0] => Array
(
[0] => 33
[user_id] => 33
[1] => 3
[frame_id] => 3
)
)
Any ideas why array_unique isn't working as expected?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这是因为
array_unique
使用字符串比较来比较项目。来自 文档:数组的字符串表示形式就是单词
Array
,无论其内容是什么。您可以通过使用以下命令来完成您想做的事情:
下面是它的工作原理:
每个数组项都被序列化。这
基于数组将是唯一的
内容。
其结果通过
array_unique
运行,所以只有具有唯一的数组
留下签名。
array_intersect_key
将采用独特物品的钥匙
map/unique 函数(因为源数组的键被保留)和 pull
它们脱离了你的原始来源
数组。
It's because
array_unique
compares items using a string comparison. From the docs:The string representation of an array is simply the word
Array
, no matter what its contents are.You can do what you want to do by using the following:
Here's how it works:
Each array item is serialized. This
will be unique based on the array's
contents.
The results of this are run through
array_unique
,so only arrays with unique
signatures are left.
array_intersect_key
will takethe keys of the unique items from
the map/unique function (since the source array's keys are preserved) and pull
them out of your original source
array.
这是 @ryeguy 的答案的改进版本:
首先,它不会执行不必要的序列化。其次,有时即使 id 相同,属性也可能不同。
这里的技巧是
array_unique()
保留键:这让我们
array_intersect_key()
保留所需的元素。我在使用 Google Places API 时遇到了这个问题。我将多个请求的结果与不同类型的对象(想想标签)结合起来。但我得到了重复项,因为一个对象可以分为多个类别(类型)。并且
serialize
方法不起作用,因为 attrs 不同,即photo_reference
和reference
。可能这些就像临时 ID。Here's an improved version of @ryeguy's answer:
First, it doesn't do unneeded serialization. Second, sometimes attributes may be different even so id is the same.
The trick here is that
array_unique()
preserves the keys:This let's
array_intersect_key()
leave the desired elements.I've run into it with Google Places API. I was combining results of several requests with different type of objects (think tags). But I got duplicates, since an object may be put into several categories (types). And the method with
serialize
didn't work, since the attrs were different, namely,photo_reference
andreference
. Probably these are like temporary ids.array_unique()
仅在 PHP 5.2.9 及更高版本中支持多维数组。相反,您可以创建数组的哈希值并检查其唯一性。
array_unique()
only supports multi-dimensional arrays in PHP 5.2.9 and higher.Instead, you can create a hash of the array and check it for unique-ness.
array_unique deos 不能递归工作,所以它只是认为“这都是数组,让我们杀死除了一个以外的所有数组……开始吧!”
array_unique deosn't work recursive, so it just thinks "this are all
Array
s, let's kill all but one... here we go!"快速回答 (TL;DR)
详细答案
上下文
问题
fname
名称-值对的行解决方案
example01 ;; DeveloperMarsher 从一个表格数据变量开始,如下所示
example01 ;; DeveloperMarsher 可以使用跟踪可见值的 foreach 循环提取不同的值
输出结果
陷阱
另请参阅
Quick Answer (TL;DR)
Detailed Answer
Context
Problem
fname
name-value pairSolution
example01 ;; DeveloperMarsher starts with a tabluar data variable that looks like this
example01 ;; DeveloperMarsher can extract distinct values with a foreach loop that tracks seen values
Output result
Pitfalls
See also
这不就起作用了吗?
Doesn't that do the trick ?