php array_unique 的奇怪行为
我正在使用以下代码来输出一个数组:
echo "======output without array_unique=====";
var_dump($selected);
echo "=====output with array_unique=====";
var_dump(array_unique($selected));die;
输出是:
======output without array_unique=====
array
0 =>
array
'uri' => string 'http://localhost/conferences/tags/0caf4c990e0a385156b33fee58e7e3fb' (length=63)
'tag' => string '1' (length=1)
'weight' => float 4
'selected' => string 'select' (length=6)
1 =>
array
'uri' => string 'http://localhost/conferences/tags/0caf4c990e0a385156b33fee58e7e3fb' (length=63)
'tag' => string '1' (length=1)
'weight' => float 4
'selected' => string 'select' (length=6)
2 =>
array
'uri' => string 'http://localhost/conferences/tags/ffc709d5131f752df8aae22d7da4240f' (length=63)
'tag' => string '2' (length=1)
'weight' => float 4
'selected' => string '' (length=0)
3 =>
array
'uri' => string 'http://localhost/conferences/tags/035c60c7f090412cc905cee008fbeba8' (length=63)
'tag' => string '3' (length=1)
'weight' => float 0
'selected' => string '' (length=0)
4 =>
array
'uri' => string 'http://localhost/conferences/tags/4137dbc16ef1a2079eb6cacb62dd8521' (length=63)
'tag' => string '4' (length=1)
'weight' => float 0
'selected' => string '' (length=0)
=====output with array_unique=====
array
0 =>
array
'uri' => string 'http://localhost/conferences/tags/0caf4c990e0a385156b33fee58e7e3fb' (length=63)
'tag' => string '1' (length=1)
'weight' => float 4
'selected' => string 'select' (length=6)
有人可以解释一下,为什么我得到的数组只有 array_unique 中的一个元素吗?
I'm using following peace of code to output an array:
echo "======output without array_unique=====";
var_dump($selected);
echo "=====output with array_unique=====";
var_dump(array_unique($selected));die;
And the output is:
======output without array_unique=====
array
0 =>
array
'uri' => string 'http://localhost/conferences/tags/0caf4c990e0a385156b33fee58e7e3fb' (length=63)
'tag' => string '1' (length=1)
'weight' => float 4
'selected' => string 'select' (length=6)
1 =>
array
'uri' => string 'http://localhost/conferences/tags/0caf4c990e0a385156b33fee58e7e3fb' (length=63)
'tag' => string '1' (length=1)
'weight' => float 4
'selected' => string 'select' (length=6)
2 =>
array
'uri' => string 'http://localhost/conferences/tags/ffc709d5131f752df8aae22d7da4240f' (length=63)
'tag' => string '2' (length=1)
'weight' => float 4
'selected' => string '' (length=0)
3 =>
array
'uri' => string 'http://localhost/conferences/tags/035c60c7f090412cc905cee008fbeba8' (length=63)
'tag' => string '3' (length=1)
'weight' => float 0
'selected' => string '' (length=0)
4 =>
array
'uri' => string 'http://localhost/conferences/tags/4137dbc16ef1a2079eb6cacb62dd8521' (length=63)
'tag' => string '4' (length=1)
'weight' => float 0
'selected' => string '' (length=0)
=====output with array_unique=====
array
0 =>
array
'uri' => string 'http://localhost/conferences/tags/0caf4c990e0a385156b33fee58e7e3fb' (length=63)
'tag' => string '1' (length=1)
'weight' => float 4
'selected' => string 'select' (length=6)
Can someone explain me, why i get array with only one element from the array_unique?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
数组元素被转换为字符串进行比较 - 这是 array_unique 手册页 中的相关片段
一旦你的数组元素被转换为字符串,它们就只有值“Array”,这当然使每个元素看起来都一样,并且你最终只得到第一个元素。
这是从像您这样的数组中删除重复项的一种方法
The array elements are cast to strings for comparison - here's the relevant snippet from the manual page for array_unique
Once your array elements are cast to strings, they simply have the value "Array", which of course makes every element look the same, and you wind up with just the first element.
Here's one way you could remove duplicates from an array like yours
我想,由于 $selected 是一个多维数组,因此 $selected[0] 与 $selected[1] 相同,都是一个数组。
据我所知, array_unique 测试 (string) $value1 === (string) $value2,所以你得到 'Array' == 'Array'。
您还没有真正解释什么使元素“唯一”(URI?)。要比较整个结构,您可能需要尝试循环遍历 $selected,序列化值(使用serialize()),然后对这些值调用 array_unique。然后,调用unserialize使数组恢复正常。
这不是最有效的解决方案,但在我担心之前,我会先进行基准测试。
请参阅:http://codepad.org/6cs5b0sm
I imagine that since $selected is a multi-dimensional array, $selected[0] is the same as $selected[1], an array.
As far as I know, array_unique tests (string) $value1 === (string) $value2, so you get 'Array' == 'Array'.
You haven't really explained what makes an element 'unique' (the URI?). To compare whole structures, you may want to try looping through $selected, serializing the values (using serialize()) and then calling array_unique on those values. Then, call unserialize to return the array to normal.
It isn't the most efficient solution, but I would benchmark first, before I worry about that.
See: http://codepad.org/6cs5b0sm
array_unique 通过比较元素的字符串值来删除重复项。数组的字符串值始终是“Array”,与数组的内容无关。
这意味着数组的所有元素都具有字符串值“Array”,因此被视为重复项,并且除了第一个元素之外都将被删除。
您应该编写自己的 array_unique 函数,该函数通过比较元素的“uri”来工作。
array_unique removes duplicates by comparing the string value of the elements. The string value of an array is always "Array", independent of the contents of the array.
This means that all the elements of your array have the string value "Array", and are therefore considered duplicates, and are removed except for the first one.
You should write your own array_unique function that works by comparing the 'uri' of the elements.
如果数组元素已经排序,您可以使用以下命令找到唯一值:
If the array elements are already sorted, you can find the unique values with this: