如何在php中获取重复的多维数组 [英] How can I get the duplicate multidimensional array in php

查看:267
本文介绍了如何在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.假设数组相等,或者数组没有不同;我的问题是,如何捕获相等的数组,目的是向用户展示有关重复输入的值,

Look at the first index (or zero) and third index (number two index), the values in a,b,c,d is equal 1,2,3,4. Assuming that the array is equal, or no different of them; my question is, how can I catch the array which equal, my purpose to show users about the value input duplicate,

我已经在使用array_unique.结果是

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

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

)

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

But I just want to get duplicate data, not remove the data duplicate.

// 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中获取重复的多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆