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

标签 php arrays multidimensional-array duplicates array-intersect

我有一个多维数组:

   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
        )

)

最佳答案

// 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
        )

)

关于php - 如何在 php 中获取重复的多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39222346/

相关文章:

javascript - 删除元素时循环遍历数组

java - 如何找到多维数组的值总和并替换未使用索引的值?

php - mysql获取 session 记录

php - 如何在 Controller codeigniter中调用多个存储过程?

php - 如何在PHP中正确打印对象内容?

php - 在 PHP 中生成随 secret 钥的最佳方法是什么?

将 64 位 Int 转换为 Char[](以及反之)

javascript - 如何打印从 $_POST 从 javascript 传输到 php 的数组

java - [Java]在循环内声明多维数组

php - 我如何重组这个数组以将数据压缩成 PHP 中更有用的格式?