3

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
        )

)
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
Kode Kite
  • 328
  • 5
  • 15
  • 1
    *my purpose to show users about the value input duplicate* - paraphrase your goal. You want to get only unique items or to get indexes of duplicating items?? – RomanPerekhrest Aug 30 '16 at 08:26
  • I'm sorry for paraphrase my goal, I want to get indexes of duplicating items @RomanPerekhrest – Kode Kite Aug 30 '16 at 08:31

4 Answers4

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

)
Zoe
  • 27,060
  • 21
  • 118
  • 148
Onesinus Saut
  • 314
  • 5
  • 18
0

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

RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
-1

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

Noman
  • 1,459
  • 2
  • 18
  • 38
-1

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);
Indrasis Datta
  • 8,692
  • 2
  • 14
  • 32
  • Yes we do use array_unique. Since it's not single dimensional, we need to do these adjustments by serializing and unserializing. – Indrasis Datta Aug 30 '16 at 08:37