dongrouyuan5685 2014-07-25 23:30
浏览 12
已采纳

从2D数组中删除重复项

I have an array which is 2D and I want to remove duplicates from it,

the var dump look like this 1=>abc.com. 2=>acc.com, 3=>abc.com so I want to remove the second occurence of abc.com, that is I want to remove 3=>abc.com,

I tried to use the nested loop, but the code is not working.

 foreach ($var as $m)
   {
        foreach ($var as $s)
        {
                if(isset($m['Email'])){
                    if($m['Email'] == $s['Email']){
                    echo 'matched with '.$s['Email'];
                    echo '</br>';
                    unset($s);
                    //echo $v['Email'];
                    //echo "<br>";
                }
            }
        }
   }

Am I missing something?

  • 写回答

3条回答 默认 最新

  • dsh77114 2014-07-25 23:33
    关注

    You can just use the array_unique function

    Edit: Here is the code and output from Here (Codepad)

    PHP

    <?php
    $input = array("1" => "abc.com", "2" => "acc.com", "3" => "abc.com");
    $result = array_unique($input);
    print_r($result);
    ?>
    

    Output

    Array
    (
        [1] => abc.com
        [2] => acc.com
    )
    

    Edit2: To have it remove duplicates from a specific column, just use

    $output = array_intersect_key($input, array_unique(array_column($input, 'Email')));
    

    Where $input is the complete array and 'Email' is the column you wish to remove duplicates from

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?