如何从PHP多维数组中删除重复值?

示例数组:

Array
(
    [0] => Array
    (
        [0] => abc
        [1] => def
    )

    [1] => Array
    (
        [0] => ghi
        [1] => jkl
    )

    [2] => Array
    (
        [0] => mno
        [1] => pql
    )

    [3] => Array
    (
        [0] => abc
        [1] => def
    )

    [4] => Array
    (
        [0] => ghi
        [1] => jkl
    )

    [5] => Array
    (
        [0] => mno
        [1] => pql
    )

)

当前回答

另一种方式。也会保存密钥。

function array_unique_multidimensional($input)
{
    $serialized = array_map('serialize', $input);
    $unique = array_unique($serialized);
    return array_intersect_key($input, $unique);
}

其他回答

这里有另一种方法。不保存中间变量。

我们使用它来消除来自各种重叠查询的重复结果。

$input = array_map("unserialize", array_unique(array_map("serialize", $input)));

序列化和惟一的替代方案

$test = [ ['abc','def'], ['ghi','jkl'], ['mno','pql'], ['abc','def'], ['ghi','jkl'], ['mno','pql'], ]; $result = array_reduce( $test, function($carry,$item){ if(!in_array($item,$carry)) { array_push($carry,$item); } return $carry; }, [] ); var_dump($result); /* php unique.php array(3) { [0] => array(2) { [0] => string(3) "abc" [1] => string(3) "def" } [1] => array(2) { [0] => string(3) "ghi" [1] => string(3) "jkl" } [2] => array(2) { [0] => string(3) "mno" [1] => string(3) "pql" } } */

如果"remove duplicate "的意思是"remove duplicate,但保留一个",一个解决方案可能是首先在"identifier column"上应用array_unique(…),然后在原始数组中删除所有已从列数组中删除的键:

$array = [
    [
        'id' => '123',
        'foo' => 'aaa',
        'bar' => 'bbb'
    ],
    [
        'id' => '123',
        'foo' => 'ccc',
        'bar' => 'ddd'
    ],
    [
        'id' => '567',
        'foo' => 'eee',
        'bar' => 'fff'
    ]
];

$ids = array_column($array, 'id');
$ids = array_unique($ids);
$array = array_filter($array, function ($key, $value) use ($ids) {
    return in_array($value, array_keys($ids));
}, ARRAY_FILTER_USE_BOTH);

结果是:

Array
(
    [0] => Array
        (
            [id] => 123
            [foo] => aaa
            [bar] => bbb
        )

    [2] => Array
        (
            [id] => 567
            [foo] => eee
            [bar] => fff
        )

)

一个简单的解决方案,可能不是最有效的:

function arrayUnique($myArray){
    if(!is_array($myArray))
        return $myArray;

    foreach ($myArray as &$myvalue){
        $myvalue=serialize($myvalue);
    }

    $myArray=array_unique($myArray);

    foreach ($myArray as &$myvalue){
        $myvalue=unserialize($myvalue);
    }

    return $myArray;

} 

对此,array_unique()文档上的用户注释提供了许多解决方案。这是其中之一:

kenrbnsn at rbnsn dot com 27-Sep-2005 12:09 Yet another Array_Unique for multi-demensioned arrays. I've only tested this on two-demensioned arrays, but it could probably be generalized for more, or made to use recursion. This function uses the serialize, array_unique, and unserialize functions to do the work. function multi_unique($array) { foreach ($array as $k=>$na) $new[$k] = serialize($na); $uniq = array_unique($new); foreach($uniq as $k=>$ser) $new1[$k] = unserialize($ser); return ($new1); }

这来自http://ca3.php.net/manual/en/function.array-unique.php#57202。