且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP得到一个多维数组的独特价值

更新时间:2023-02-16 16:12:41

array_unique 使用比较值之前串转换,找到独特的价值观:

array_unique is using string conversion before comparing the values to find the unique values:

注意:有两个因素被认为是平等的,当且仅当(字符串)$ elem1 ===(字符串)$ elem2时。在话:当字符串重新presentation是一样的。第一元件将被使用。

Note: Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same. The first element will be used.

但是,一个阵列总是会转换为阵列

But an array will always convert to Array:

var_dump("Array" === (string) array());

您可以通过指定解决这个问题的的 SORT_REGULAR 的中的第二个参数模式 array_unique

You can solve this by specifying the SORT_REGULAR mode in the second parameter of array_unique:

$unique = array_unique($a, SORT_REGULAR);

或者,如果不工作,由之前序列化阵列和的反序列化该调用后 array_unique 来找到的唯一值:

Or, if that doesn’t work, by serializing the arrays before and unserializing it after calling array_unique to find the unique values:

$unique = array_map('unserialize', array_unique(array_map('serialize', $a)));