如何从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
    )

)

当前回答

这个解决方案只在一个数组列需要唯一性时才有用,例如,如果我们需要数组索引#0中的唯一性。

解决方案1:

使用array_filter和一个匿名函数和一个静态变量:

<?php

$list = [
    ['abc', 'def'],
    ['ghi', 'jkl'],
    ['mno', 'pql'],
    ['abc', 'def'],
    ['ghi', 'jkl'],
    ['mno', 'pql']
];

$list = array_filter($list, function ($item) {
    static $values = [];
    if (!in_array($item[0], $values)) {
        $values[] = $item[0];
        return true;
    } else {
        return false;
    }
});

var_dump($list);

解决方案2:

由于我们想要唯一性的值是字符串类型,我们可以重新映射原始全局数组,使用这些值作为键,这将在我们重新映射时删除重复的值:

<?php

$list = [
    ['abc', 'def'],
    ['ghi', 'jkl'],
    ['mno', 'pql'],
    ['abc', 'def'],
    ['ghi', 'jkl'],
    ['mno', 'pql']
];

$unique = [];

foreach ($list as $item) {
    $unique[$item[0]] = $item;
}

// Remap again to integers
$unique = array_values($unique);

var_dump($unique);

其他回答

对此,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。

如果您需要消除特定键上的重复项,例如mysqli id,这里有一个简单的函数

function search_array_compact($data,$key){
    $compact = [];
    foreach($data as $row){
        if(!in_array($row[$key],$compact)){
            $compact[] = $row;
        }
    }
    return $compact;
}

加分 您可以传递一个键数组并添加一个外部foreach,但每增加一个键将慢2倍。

如果"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 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)));