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 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi