如何在 php 中获取重复的多维数组

How can I get the duplicate multidimensional array in php

我有一个多维数组:

   Array
(
    [0] => Array
        (
            [a] => 1
            [b] => 2
            [c] => 3
            [d] => 4
        )

    [1] => Array
        (
            [a] => 1
            [b] => 5
            [c] => 3
            [d] => 4
        )

    [2] => Array
        (
            [a] => 1
            [b] => 2
            [c] => 3
            [d] => 4
        )

)

查看第一个索引(或零)和第三个索引(第二个索引),a,b,c,d 中的值等于 1,2,3,4。假设数组相等,或者它们没有区别;我的问题是,我怎样才能捕捉到相等的数组,我的目的是向用户展示重复的值输入,

我已经在使用 array_unique。这是结果:

    Array
(
    [0] => Array
        (
            [a] => 1
            [b] => 2
            [c] => 3
            [d] => 4
        )

    [1] => Array
        (
            [a] => 1
            [b] => 5
            [c] => 3
            [d] => 4
        )

)

但我只是想获取重复的数据,而不是去除重复的数据。

// first : get all data, if the data same / duplicate take only one data

$unique = array_unique($data, SORT_REGULAR);

// then, get the data which duplicate with

$diffCellUniq = array_diff_key($data, $unique);

// so print the result 

print_r($diffCellUniq); exit;



   Array
(
    [2] => Array
        (
            [a] => 1
            [b] => 2
            [c] => 3
            [d] => 4
        )

)

你必须使用 array_unique:

$var = array_unique($YOUR ARRAY, SORT_REGULAR);

编辑:

<?php
$your_array = array (
    0 => array
        (
            'a' => '1',
            'b' => '2',
            'c' => '3',
            'd' => '4',
        ),

    1 => array
        (
            'a' => '1',
            'b' => '5',
            'c' => '3',
            'd' => '4',
        ),

    2 => array
        (
            'a' => '1',
            'b' => '2',
            'c' => '3',
            'd' => '4',
        ),  
);

$tmpArray = array ();

foreach ($your_array as $row) 
    if (!in_array($row, $tmpArray)) array_push($tmpArray, $row);

var_dump($tmpArray);
?>

同时查看在线示例https://eval.in/631417

由于 array_unique 不适用于多维数组,实现此目的的一种方法是序列化此数组并对其应用 array_unique 函数。

应用唯一函数后,我们可以反序列化它并以数组格式取回它。

参考:http://php.net/manual/en/function.array-unique.php#97285

试试这个:

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

要仅复制保留其键的项目 - 使用以下方法和 array_keysarray_fill_keysarray_mergearray_uniquearray_intersect_key 函数:

$dup_keys = [];
foreach ($arr as $item) {  // $arr is your initial array
    $keys = array_keys($arr, $item);
    if (count($keys) > 1) {
        $dup_keys = array_merge($dup_keys, $keys);
    }
}

$dup_keys = array_unique($dup_keys);
$dup_items = array_intersect_key($arr, array_fill_keys($dup_keys, 0));

print_r($dup_items);

输出:

Array
(
    [0] => Array
        (
            [a] => 1
            [b] => 2
            [c] => 3
            [d] => 4
        )

    [2] => Array
        (
            [a] => 1
            [b] => 2
            [c] => 3
            [d] => 4
        )
)

DEMO link

// first : get all data, if the data same / duplicate take only one data

$unique = array_unique($data, SORT_REGULAR);

// then, get the data which duplicate with

$diffCellUniq = array_diff_key($data, $unique);

// so print the result

print_r($diffCellUniq); exit;

Array
(
    [2] => Array
        (
            [a] => 1
            [b] => 2
            [c] => 3
            [d] => 4
        )

)