在多维数组中查找重复值



我有一个数组,看起来像这样:

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

我想有一种方法来检查php中键[1](John Doe/Jane Doe键(和[2](SNxxxx键(的重复值,但忽略键[0]的重复值。如何才能做到这一点?

这里已经回答了这个问题。以下是该问题已接受答案的代码。

它使用array_intersect()函数。

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

输出:

阵列([0]=>测试数据(

试试这个:

$array = your_array();
$current = current($array);
foreach($array as $key => $val){
 $duplicate[$key] = array_intersect($current, $val);
}
echo($duplicate);
<?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;

这将为您提供密钥上的副本。你可以试试类似的

你可以在这里查看-https://3v4l.org/hBtQt

相关内容

  • 没有找到相关文章

最新更新