0

I have an array that looks similar to this:

Array
(
    [0] => Array
        (
            [0] => Model2345
            [1] => John Doe
            [2] => SN1234
        )

    [1] => Array
        (
            [0] => Model2345
            [1] => John Doe
            [2] => SN3456
        )

    [2] => Array
        (
            [0] => Model1234
            [1] => Jane Doe
            [2] => SN3456
        )
)

I want to have a way to check for duplicate values for keys [1] (the John Doe/Jane Doe key) and [2] (the SNxxxx key) in php, but ignore duplicates for key [0]. How can this be accomplished?

Wes
  • 724
  • 1
  • 11
  • 29

3 Answers3

2

This question has already been answered here. The following is the code from the accepted answer of that question.

It utilizes the array_intersect() function.

<?php
$array = array(array("test data","testing data"), array("new data","test data"), array("another data", "test data", "unique data"));
$result = array();

$first = $array[0];
for($i=1; $i<count($array); $i++)
{
    $result = array_intersect ($first, $array[$i]);
    $first = $result;
}
print_r($result);
?>

OUTPUT:

Array ( [0] => test data )

Community
  • 1
  • 1
Acharya Anurag
  • 671
  • 6
  • 23
  • I tried this with my array but get an empty result even when there are clearly duplicates. Is that because your example is with integers and not strings? – Wes Aug 26 '15 at 07:03
  • I tested the code and it works just fine for strings as well. I have updated the code with the **exact** code that worked for me. Of course, this only works if there is a common value in **all three** sub-arrays, meaning it would show empty result for your example array. Are you looking for a to catch even a single repetition or just across all arrays? – Acharya Anurag Aug 26 '15 at 07:36
  • Ah I see - so this wouldn't work for me. I need to catch even a single repetition. – Wes Aug 26 '15 at 13:43
  • It seems to be fine, Thanks! – Mostafa Soufi May 27 '20 at 12:26
0

Try this:

$array = your_array();

$current = current($array);
foreach($array as $key => $val){
 $duplicate[$key] = array_intersect($current, $val);
}

echo($duplicate);
jitendrapurohit
  • 9,435
  • 2
  • 28
  • 39
  • This almost works, but it always prints the first array regardless if there are duplicates or not. – Wes Aug 26 '15 at 07:03
0
<?php

$Contacts = [
  [
    'name' => 'name 1',
    'phone' => '12341234',
    'email' => 'test@web.com'
  ],
  [
    'name' => 'name 1',
    'phone' => '12341234',
    'email' => 'test@web.com'
  ],
  [
    'name' => 'name 3',
    'phone' => '4322342',
    'email' => 'test@web1.com'
  ],
  [
    'name' => 'name 4',
    'phone' => '1234123423',
    'email' => 'test@web1.com'
  ],
  [
    'name' => 'name 5',
    'phone' => '12341266634',
    'email' => 'test@eqweqwweb.com'
  ],
];

$start = microtime(true);   

    // function CheckDuplicteContacts($Contacts)
    // {
        if(count($Contacts) == 1){
            return $validator = true;
        } else {
            $DuplicateLines = [];
            foreach($Contacts as $CurrentKey => $Contact)
            {
                foreach($Contacts as $SearchKey => $SearchContact)
                {
                    
                    if(
                        (
                            ($SearchContact["name"] == $Contact["name"]) &&
                            ($SearchContact["email"] == $Contact["email"]) &&
                            ($SearchContact["phone"] == $Contact["phone"])
                        )
                        && 
                        ($SearchKey != $CurrentKey))
                    {
                        array_push($DuplicateLines,$CurrentKey + 1);
                    }
                }
            }
            $validator = empty($DuplicateLines) ? true : $DuplicateLines;

        }
  //  }
    
 
var_dump($validator);

echo (microtime(true) -$start)*100;

This gives you the duplicates on keys. You can try something similar

You can check here - https://3v4l.org/hBtQt