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

)

当前回答

序列化和惟一的替代方案

$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" } } */

其他回答

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

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

从5.2.9开始,如果你像这样使用SORT_REGULAR标志,你可以使用array_unique():

array_unique($array, SORT_REGULAR);

这使得函数比较元素是否相等,就像使用了$a == $b一样,这非常适合您的情况。

输出

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

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

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

)

但是请记住,文档声明:

Array_unique()并不用于多维数组。

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

)
Array
(
    [0] => Array
        (
            [id] => 1
            [name] => john
        )

    [1] => Array
        (
            [id] => 2
            [name] => smith
        )

    [2] => Array
        (
            [id] => 3
            [name] => john
        )

    [3] => Array
        (
            [id] => 4
            [name] => robert
        )

)

$temp = array_unique(array_column($array, 'name'));
$unique_arr = array_intersect_key($array, $temp);

这将从数组中删除重复的名称。按键唯一

如果你有一个这样的数组:

(users是数组的名称)

Array=>
 [0] => (array)
   'user' => 'john'
   'age' => '23'
 [1] => (array)
  'user' => 'jane'
  'age' => '20'
 [2]=> (array)
  'user' => 'john'
  'age' => '23'

如果你想删除重复的文件,那么:

$serialized = array();
for ($i=0; $i < sizeof($users); $i++) { 
  $test = in_array($users['user'], $serialized);
    if ($test == false) {
      $serialized[] = $users['user'];
    }
 }

可以是一个解决方案:P