【问题标题】:How to detect duplicate values in multidimensional associative array?如何检测多维关联数组中的重复值?
【发布时间】:2017-06-17 12:52:07
【问题描述】:

我有一个关联多维数组:

Array
(
    [0] => Array
        (
            [customer_name] => John Dow
            [customer_email] => john@example.com
            [customer_mobile] => 1236547895
            [birth_date] => 12/1/1996
            [status] => Enable
        )

    [1] => Array
        (
            [customer_name] => Alex
            [customer_email] => alex@example.com
            [customer_mobile] => 4563214785
            [birth_date] => 19/1/1996
            [status] => Enable
        )

    [2] => Array
        (
            [customer_name] => Arina
            [customer_email] => arina@example.com
            [customer_mobile] => 963214785
            [birth_date] => 25/1/1996
            [status] => Enable
        )

    [3] => Array
        (
            [customer_name] => Atom
            [customer_email] => atom@example.com
            [customer_mobile] => 5214789632
            [birth_date] => 12/1/1998
            [status] => Enable
        )

    [4] => Array
        (
            [customer_name] => Jennifer
            [customer_email] => jennifer@example.com
            [customer_mobile] => 4563214785
            [birth_date] => 12/2/1996
            [status] => Enable
        )
)

现在我想检查 customer_mobilecustomer_email 中的相似值以减少冗余。联系电话和电子邮件地址必须是非冗余的。

所以请指导我,我怎样才能做到这一点?谢谢:)

【问题讨论】:

  • 发布想要的结果
  • 我不想要任何结果。我只是想检查是否有任何客户有重复的联系电话和电子邮件地址。它作为标志返回 - true 表示该数组包含冗余
  • 你写的 it return as flag - 一个标志用于所有项目或每个项目单独?
  • 所有项目的标志 - 如果 1 个联系号码重复 2 次或更多次以及电子邮件。整个数组将被丢弃

标签: php arrays multidimensional-array array-unique


【解决方案1】:

因为你不需要知道which,而只需要知道if,你可以使用array_column + array_unique: (run)

$cm = array_column($arr, 'customer_mobile');
if($cm != array_unique($cm)){
    echo 'There are duplicates in customer_mobile';
}

$ce = array_column($arr, 'customer_email');
if($cm != array_unique($ce)){
    echo 'There are duplicates in customer_email';
}

如果您需要同时匹配电子邮件和手机,请在同一个if

if($cm != array_unique($cm) && $ce != array_unique($ce)){
    echo 'There are duplicates in both customer_mobile and customer_email';
}

【讨论】:

    【解决方案2】:

    简单的解决方案是:

    <?php
    
    $data = [
      [
        'name' => 'name 1',
        'phone' => '12341234',
        'email' => 'test@web.com'
      ],
      [
        'name' => 'name 2',
        'phone' => '12341234',
        'email' => 'test@web1.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'
      ],
    ];
    
    $phones = [];
    $emails = [];
    foreach ($data as $key => $contact) {
      if (array_search($contact['phone'], $phones) !== false || array_search($contact['email'], $emails) !== false) {
        unset($data[$key]);
      } else {
        $phones[] = $contact['phone'];
        $emails[] = $contact['email'];
      }
    }
    
    var_dump($data);
    

    结果你会得到:

    array(3) {
      [0] =>
      array(3) {
        'name' =>
        string(6) "name 1"
        'phone' =>
        string(8) "12341234"
        'email' =>
        string(12) "test@web.com"
      }
      [2] =>
      array(3) {
        'name' =>
        string(6) "name 3"
        'phone' =>
        string(7) "4322342"
        'email' =>
        string(13) "test@web1.com"
      }
      [4] =>
      array(3) {
        'name' =>
        string(6) "name 5"
        'phone' =>
        string(11) "12341266634"
        'email' =>
        string(18) "test@eqweqwweb.com"
      }
    }
    

    这只是一个例子。

    【讨论】:

      【解决方案3】:

      foreach 试试这个。数组只需要遍历一次,使用 email 和 mobile 作为唯一键,唯一键相同的元素只保留最后一个。如果您希望结果使用数字索引,请在$result 上使用array_values()

      $result = [];
      foreach($array as $v)
      {
        $result[$v['customer_email'] . $v['customer_mobile']] = $v;
      }
      

      【讨论】:

        【解决方案4】:

        你可以这样做(我从头生成代码,所以它可能有错误 - 但想法应该很清楚)(我假设你的数组名称是 $persons):

        $emails = [];
        $mobiles = [];
        
        $discard = false;
        foreach($persons as $person) 
        {
           $email = $person['customer_email'];
        
           if(!isset($emails[$email])) {
               $emails[$email] = $person;
           } else {
              $emails[$email]['redundant_email']=true;
              $person['redundant_email']=true;
              $discard = true;
           }
        
           $mobile = $person['customer_mobile'];
        
           if(!isset($mobiles[$mobile])) {
               $mobiles[$mobile] = $person;
           } else {
               $mobiles[$mobile]['redundant_mobile']=true;
               $person['redundant_mobile']=true;
               $discard = true;
           }
        }
        

        因此,每个拥有冗余手机或电子邮件的人都将字段 redundant_emailredundant_mobile 设置为 true。变量$discard=true 表示该数组是多余的。

        【讨论】:

        • 数组中没有任何值..!
        • 如果 1 个联系电话重复 2 次或更多次以及电子邮件。整个数组将被丢弃..我怎样才能做到这一点?
        • @DevendraSingh - 由于您的新要求,我再次进行更正 - 现在您在 $discard 变量中拥有布尔答案以及冗余人员中的“标志”
        【解决方案5】:

        我的回答是,你根本不应该在 PHP 中这样做。在您提出的情况下,应仅在数据库端检查/验证/过滤数据。如果有重复,那么您根本不需要获取数据!

        运行查询以检查 db 中的冗余。只有在没有冗余的情况下才能获取数据。

        如果有很多数据,那么您将节省大量数据提取并从头开始循环数据。

        祝你好运。

        【讨论】:

          【解决方案6】:

          这是我的解决方案,工作正常。

              $name = array_column($array, 'name');
              $filteredKeys = array_unique($name);
          
              foreach (array_keys($filteredKeys) as $key => $value) {
              $filtered [] = $array[$value];
              }
                return  $filtered;
              }
          

          【讨论】:

            猜你喜欢
            • 2021-09-25
            • 1970-01-01
            • 1970-01-01
            • 2018-04-25
            • 2015-11-09
            • 2013-07-30
            • 1970-01-01
            • 2021-05-01
            • 2021-12-13
            相关资源
            最近更新 更多