PHP 按值唯一数组
我在 PHP 中有一个数组,如下所示:
[0]=>
array(2) {
["name"]=>
string(9) "My_item"
["url"]=>
string(24) "http://www.my-url.com/"
}
[1]=>
array(2) {
["name"]=>
string(9) "My_item"
["url"]=>
string(24) "http://www.my-url2.com/"
}
“name”中的两个值在这两项中是相同的。我想像这样整理重复的内容。
如何通过检查“名称”值来创建唯一的数组?
I have an array in PHP that looks like this:
[0]=>
array(2) {
["name"]=>
string(9) "My_item"
["url"]=>
string(24) "http://www.my-url.com/"
}
[1]=>
array(2) {
["name"]=>
string(9) "My_item"
["url"]=>
string(24) "http://www.my-url2.com/"
}
The two values in "name" are the same in this two items. I want to sort out duplicates like this.
How do I create an unique array by checking the "name" value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
基本上
Basically
序列化对于简化建立分层数组唯一性的过程非常有用。使用这一行来检索仅包含唯一元素的数组。
Serialisation is very useful for simplifying the process of establishing the uniqueness of a hierarchical array. Use this one liner to retrieve an array containing only unique elements.
请发现此链接很有用。它使用 MD5 哈希来检查重复项:
http://www.phpdevblog.net/2009/01/using-array-unique-with-multiDimension-arrays.html
快速浏览:
请参阅此处的工作示例:
http://codepad.org/9nCJwsvg
Please find this link useful. It uses an MD5 hash to examine the duplicates:
http://www.phpdevblog.net/2009/01/using-array-unique-with-multidimensional-arrays.html
Quick Glimpse:
See the working example here:
http://codepad.org/9nCJwsvg
简单的解决方案:
Simple Solution:
鉴于数组 (0,1) 上的键似乎并不重要,一个简单的解决方案是使用“name”引用的元素的值作为外部数组的键:
...并且如果有除了“名称”之外只有一个值,为什么还要使用嵌套数组呢?
Given that the keys on the array (0,1) do not seem to be significant a simple solution would be to use the value of the element referenced by 'name' as the key for the outer array:
...and if there is only one value other than the 'name' why bother with a nested array at all?