How can I get the duplicate multidimensional array in php

北城以北 提交于 2019-12-11 06:29:58

问题


I have an multidimensional 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
        )

)

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,

I have already been using array_unique. This is the result :

    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
        )

)

回答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
        )

)



回答2:


To get only duplicating items preserving their keys - use the following approach with array_keys, array_fill_keys, array_merge, array_unique and array_intersect_key functions:

$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);

The output:

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

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

DEMO link




回答3:


Since array_unique doesn't work on multi-dimensional array, one way to achieve this is to serialize this array and apply array_unique function on this.

Once unique function is applied, we can unserialize it and get it back in array format.

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

Try this:

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



回答4:


You have to use array_unique:

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

Edit:

<?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);
?>

Check online example as well https://eval.in/631417



来源:https://stackoverflow.com/questions/39222346/how-can-i-get-the-duplicate-multidimensional-array-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!