我怎样才能在PHP中重复的多维数组(How can I get the duplicate mult

2019-09-30 10:38发布

我有一个多维数组:

   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
        )

)

Answer 1:

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

)


Answer 2:

要获得唯一的复制项目维护自己的钥匙-使用下面的方法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
        )
)

演示链接



Answer 3:

由于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);


Answer 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



文章来源: How can I get the duplicate multidimensional array in php