doufendi9063 2017-07-19 06:42
浏览 174
已采纳

检查数组中是否包含多个相同的值[关闭]

I have a array like below

$array1 = Array
(
    [0] => 20225,2017-02-20
    [1] => 20225,2017-02-20
    [2] => 10027,2017-02-20
    [3] => 10027,2017-02-20
    [4] => 10021,2017-02-20
    [5] => 20205,2017-02-20
    [6] => 50003,2017-02-20
    [7] => 20225,2017-02-20
    [8] => 20205,2017-02-20
    [9] => 10021,2017-02-20
    [10] => 50003,2017-02-20
    [11] => 10027,2017-02-20
    [12] => 20225,2017-02-20
    [13] => 20225,2017-02-21
    [14] => 20225,2017-02-21
)

if the array contain same result more than 2, I want to get one of those values to another array called $array2.

Please help me to do this

  • 写回答

4条回答 默认 最新

  • dpcyx08288 2017-07-19 06:45
    关注

    As You said:- if the array contain same result more than 2, I want to get one of those values to another array called $array2.

    You can do it like below:-

    $array_new = array_count_values($array1);
    $array2 = array();
    foreach($array_new as $key=>$val){
        if($val >1){ //or do $val >2 based on your desire
          $array2[] = $key;
        }
    }
    print_r($array2);
    

    Output:- https://eval.in/834306 OR https://eval.in/834402

    If you want to check that array contains duplicates or not then you can do like this:-

    if(count(array_unique($array1)) < count($array1)){
      echo "Array have some duplicates";
    }else{
      echo "Array have unique elements";
    }
    

    Output:-https://eval.in/834312

    If you want to get only unique array then you can do like below:-

    $array1 = array_values (array_unique($array1));
    

    Output:- https://eval.in/834317

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

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作